打印「一」是正確的,如果你想打印「樵夫」,附加到字母列表中的那些字符(即變量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
當描述與代碼非常不同時,很難猜出您需要什麼。你寫的是「列表」,但代碼中沒有任何內容。 –