清单:如何判断列表中的字母是否在Word中?

Bob*_*Bob 1 python for-loop list python-3.x

例如,如果您有一个列表

C=''
B='apple'
A=['a','b','c','d','e']
Run Code Online (Sandbox Code Playgroud)

如何查看该列表中的任何字母是否在"apple"中,向用户显示其中包含的字母,并为列表中未显示的每个字母显示" - ".例如,苹果将是一个--- e.我以为它会像......

for item in A:
     if item in B:
          C+=item
     else:
          C+='-'
print(C)
Run Code Online (Sandbox Code Playgroud)

但我无法弄明白.任何和所有的帮助表示赞赏.

jam*_*lak 8

>>> B = 'apple'
>>> A = ['a','b','c','d','e']
>>> print ''.join(c if c in B else '-' for c in A)
a---e
Run Code Online (Sandbox Code Playgroud)

这相当于这个for循环:

>>> s = ''
>>> for c in A:
        if c in B:
            s += c
        else:
            s += '-'


>>> print s
a---e
Run Code Online (Sandbox Code Playgroud)