[英]store loop result in variable,python
i have a list of sentences called "data" and i carried out an operation of soundex. 我有一个称为“数据”的句子列表,我进行了soundex运算。
i dont know how to store it in a variable.. here is my code: 我不知道如何将其存储在变量中。这是我的代码:
for line in data:
for word in line.split():
print jellyfish.soundex(word)
that gives me a list of soundex codes for all words.. 这给了我所有单词的soundex代码列表。
how do i store the results in a variable?? 如何将结果存储在变量中? i have tried: 我努力了:
data_new = [(jellyfish.soundex(word) for word in line.split()) for line in data]
but that did not help. 但这没有帮助。
从理解范围内删除生成器表达式:
data_new = [jellyfish.soundex(word) for line in data for word in line.split() ]
Use list comprehension instead of generator expression: 使用列表推导而不是生成器表达式:
data_new = [[jellyfish.soundex(word) for word in line.split()] for line in data]
Or, if you want flat list: 或者,如果您想要平面清单:
data_new = [jellyfish.soundex(word) for line in data for word in line.split()]
Remove the parentheses around (jellyfish.soundex(word) for word in line.split())
, which is a generator expression (see for example generator comprehension ). 删除(jellyfish.soundex(word) for word in line.split())
括号,它是一个生成器表达式(例如,参见generator comprehension )。 The result, 结果,
data_new = [jellyfish.soundex(word) for word in line.split() for line in data]
should give you what you seem to want. 应该给你你想要的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.