我有一种感觉,我会被告知去“初学者指南”或你有什么,但是我有这里的代码为第三个列表中给定数量的元素返回两个列表之间的字符串匹配
does = ['my','mother','told','me','to','choose','the']
it = ['my','mother','told','me','to','choose','the']
work = []
while 5 > len(work):
for nope in it:
if nope in does:
work.append(nope)
print (work)
我也得到
['my', 'mother', 'told', 'me', 'to', 'choose', 'the']
这是为什么?我如何说服它返回
['my', 'mother', 'told', 'me']
这就好比一个交集(截断),尽管集合没有顺序。 – smci
请注意,使用'while 5> len(work)'命令被许多人看作是不合逻辑的,导致了[“yoda conditions”](https://en.wikipedia.org/wiki/Yoda_conditions)的名称。这当然是正确的任何方式:) –
@WilliamCorrigan你应该接受你发现的答案,有助于向其他读者指出什么帮助解决了你的问题。 – idjaw