list_of_food = []
def split_food(input):
#split the input
words = input.split()
for i in words:
list_of_food = list_of_food.append(i)
print list_of_food
>>> text = "What can I say about this place. The staff of these restaurants is nice and the eggplant is not bad.'
>>> txt1 = text.split('.')
>>> txt2 = [line.split() for line in txt1]
>>> new_list = []
>>> for i in range(0, len(txt2)):
l1 = txt2[i]
for w in l1:
new_list.append(w)
print(new_list)
使用“extend”关键字。这将两个列表聚合在一起。
你应该把这个改成
有两个不同的原因。首先,
list.append()
是一个就地运算符,因此在使用列表时不必担心重新分配列表。其次,当您试图在函数中使用全局变量时,您要么需要将其声明为global
,要么永远不要为其赋值。否则,您将要做的唯一事情就是修改本地。这可能是你想用你的函数做的。但是,因为除非绝对必要(这不是一个很好的实践),否则不应该使用globals,所以这是最好的方法:
相关问题 更多 >
编程相关推荐