Advanced Python list comprehension

后端 未结 6 2393
北海茫月
北海茫月 2021-02-20 14:02

Given two lists:

chars = [\'ab\', \'bc\', \'ca\']
words = [\'abc\', \'bca\', \'dac\', \'dbc\', \'cba\']

how can you use list comprehensions to

6条回答
  •  别那么骄傲
    2021-02-20 14:37

    Using zip:

    [w for w in words if all([a in c for a, c in zip(w, chars)])]
    

    or using enumerate:

    [w for w in words if not [w for i, c in enumerate(chars) if w[i] not in c]]
    

提交回复
热议问题