var s = "overpopulation";
var ar = [];
ar = s.split();
alert(ar);
我想把一个单词string.split成一个字符数组。
上面的代码似乎不起作用-它将"overpopulation“作为Object返回。
如果原始字符串不包含逗号和空格,我如何将其拆分成字符数组?
我有一个字符串,I在每一行上都有单词。我正在尝试编写一个for循环,以逐字或逐行打印每个单词。 String =
"Hello
How
Are
you"
for x in String:
print(x) 可悲的是我得到了 H
e
l
l
o
H
o
w
A
r
e
Y
o
u 而不是这个? Hello
How
Are
You 哪里出了问题?
我希望我的Python函数拆分一个句子(输入),并将每个单词存储在一个列表中。我当前的代码拆分句子,但没有将单词存储为列表。我该怎么做?
def split_line(text):
# split the text
words = text.split()
# for each word in the line:
for word in words:
# print the word
print(words)
我怎样才能把一个包含标点符号的单词分成两个不带标点符号的单词呢?例如,如果我有一个字符串"half-attained",我如何使它可以去掉"-“,以及将单词拆分成"half”和"attained“。
这就是我到目前为止所做的,它只删除了标点符号,并将单词放在一起。
for n in range(0,len(test_list)):
no_punct = ""
for char in test_list[n]:
if char not in punctuations:
no_punct = no_punct
我刚刚编写了一些代码,这些代码应该遍历文件的每一行,如果行的第一个单词后面的数字是1,则应该调用另一个函数。
该文件如下所示:
Button0 1
Button1 %0
运动0
命令0
我的代码:
currLog = open(log,"r") #Open current Log-File
for line in currLog: #Loop through each Line
pos = line.split(" ") #Split line into Name(pos0) and Answer(pos1)
p
我正在尝试将一个文本文件读入列表中。当它读取由逗号分隔的单词列表时,列表的末尾有'‘和'\n’。我添加了strip(),但它只删除了'\n‘。 with open("tasks.txt",'r') as file:
for line in file:
word = line.strip().split(',')
if word[0] == username:
view_list.append(word) 循环可以工作,除了在
有人能解释一下python 2.7.8上的这种行为吗?
Python 2.7.8 (default, Nov 12 2014, 02:03:09)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = ''
>>> a.split()
[]
>>> a.spli
你好,我正在学习python,当我找到列表时,我决定尝试这样的方法:
接收用户全名的输入和返回姓氏的打印以表示欢迎。
nome = []
nome = input("Enter your full name ")
print("Welcome Mr. ", nome[2] )
但是input将每个字符作为列表中的一个新项插入,因此结果是"I“,因为我输入了"LOIP”,而"I“是列表中的第2号。
详细说明:
[0] [1] [2] [3]
[L] [O] [I] [P]
但我想打印的是第二个单词而不是名单
所以我有一个由空格和制表符分隔的巨大的单词字符串,我想知道我可以做些什么来快速地将每个单词添加到列表中。
例如。
x = "hello Why You it from the"
list1 = ['hello', 'why', 'you', 'it','from', 'the']
字符串有制表符和单词之间的多个空格,我只需要一个快速的解决方案,而不是手动修复这个问题