这是我的代码:
from functools import partial
def x_in_y(word, inner):
return inner in word
wrong = ['mann','connaction','tee','rigt','putt']
sentence=['this mann is my son','the connaction is unstable','my tee is getting cold','put your hands down','rigt now','right behind my back']
for i in wrong:
print(f"Wrong: {i}")
filtered_names = filter(partial(x_in_y, inner=i), sentence)
for name in filtered_names:
print(name)
请告诉我如何仅显示比赛而不显示其他比赛? (在这种情况下,请删除“ wrong:putt”)
赞:
Wrong: mann
this mann is my son
Wrong: connaction
the connaction is unstable
Wrong: tee
my tee is getting cold
Wrong: rigt
rigt now
请帮助我。
答案 0 :(得分:0)
如果您只希望打印名称出现在至少一个句子中,只需将print(f"Wrong: {i}")
移动到filtered_names
下方,并使用next()
检查为空的迭代器,如下所示:>
for i in wrong:
filtered_names = filter(partial(x_in_y, inner=i), sentence)
next_elem = next(filtered_names, None)
if next_elem:
print(f"Wrong: {i}")
print(next_elem)
for name in filtered_names:
print(name)
答案 1 :(得分:0)
简单地嵌套循环呢?
#!/usr/bin/env python
wrong = ["mann", "connaction", "tee", "rigt", "putt"]
sentences = [
"this mann is my son",
"the connaction is unstable",
"my tee is getting cold",
"put your hands down",
"rigt now",
"right behind my back",
]
for word in wrong:
for sentence in sentences:
if word in sentence:
print(f"Wrong: {word}")
print(sentence)
答案 2 :(得分:0)
如何在每个标签旁边添加一个正确的单词?则表示每个结果的正确单词。
喜欢这个:
Wrong: mann "should be man"
this mann is my son
Wrong: connaction "should be connection"
the connaction is unstable
Wrong: tee "should be tea"
my tee is getting cold
Wrong: rigt "should be right"
rigt now