打印“一”是正确的,如果你想打印“樵夫”,附加到字母列表中的那些字符(即变量y)
y = 'albumjcker' # all characters inside "lumberjack"
s = 'lumberjack'
for x in s:
if any(x in alpha for alpha in y):
print x
应该做的伎俩
尝试:
y = ["a", "b", "c", "l"]
s = ["banana split", "lumberjack"]
for words in s:
for char in y:
if char in words:
print (words)
break
y = ["animal","zoo","potato"]
s = ["The animal farm on the left","I had potatoes for lunch"]
for words in s:
for char in y:
if char in words:
print (words)
break
The animal farm on the left
I had potatoes for lunch
编辑
y = ["animal","zoo","potato"]
s = ["The animal farm on the left","I had potatoes for lunch"]
s = list(set(s)) # But NOTE THAT this might change the order of your original list
for words in s:
for char in y:
if char in words:
print (words)
break
如果顺序很重要,那么我想你只能做
y = ["animal","zoo","potato"]
s = ["The animal farm on the left","I had potatoes for lunch"]
new = []
for x in s:
if x not in new:
new.append(x)
s = new
for words in s:
for char in y:
if char in words:
print (words)
break
也许只是一个'如果中的Y:打印s' –
您的与'任何'似的过于复杂。这种逻辑在python中通常非常简单直接。 – keyser
当描述与代码非常不同时,很难猜出您需要什么。你写的是“列表”,但代码中没有任何内容。 –