我正在努力理解以下列表理解
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])])
相当于:
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)
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)
有人可以用简单的 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])])
相当于:
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)
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)