Advanced Python list comprehension

Posted by Yuval A on Stack Overflow See other posts from Stack Overflow or by Yuval A
Published on 2010-03-26T10:30:45Z Indexed on 2010/03/26 10:33 UTC
Read the original article Hit count: 588

Filed under:
|

Given two lists:

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

how can you use list comprehensions to generate a filtered list of words by the following condition: given that each word is of length n and chars is of length n as well, the filtered list should include only words that each i-th character is in the i string in words.

In this case, we should get ['abc', 'bca'] as a result.

(If this looks familiar to anyone, this was one of the questions in the previous Google code jam)

© Stack Overflow or respective owner

Related posts about python

Related posts about list-comprehension