[英]I am struggling with understanding the following list comprehension
可以请某人用简单的 for 循环和语句编写以下列表理解。
new_words = ' '.join([word for word in line.split() if not
any([phrase in word for phrase in char_list])])
我在下面的代码中写了上面的列表理解,但它不起作用。
new_list = []
for line in in_list:
for word in line.split():
for phrase in char_list:
if not phrase in word:
new_list.append(word)
return new_list
谢谢
new_words = ' '.join(
[
word for word in line.split()
if not any(
[phrase in word for phrase in char_list]
)
]
)
或多或少相当于:
new_list = []
for word in line.split():
phrases_in_word = []
for phrase in char_list:
# (phrase in word) returns a boolean True or False
phrases_in_word.append(phrase in word)
if not any(phrases_in_word):
new_list.append(word)
new_words = ' '.join(new_list)
new_words = ' '.join([word for word in line.split()
if not any([phrase in word for phrase in char_list])])
相当于:
lst = []
for word in line.split():
for phrase in char_list:
if phrase in word:
break
else: # word not in ANY phrase
lst.append(word)
new_words = ' '.join(lst)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.