Advanced Python list comprehension

后端 未结 6 2392
北海茫月
北海茫月 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:45

    This works, using index:

    [words[chars.index(char)] for char in chars if char in words[chars.index(char)]]
    

    Am I missing something?

提交回复
热议问题