[].index

From: Mike Edey (mike_at_edey.mine.nu)
Date: 05/31/04


Date: Mon, 31 May 2004 14:14:59 GMT

Good day.
Recently I'd run into wishing a list's index method would match
substrings. Being fairly new to this game I can't help but think that my
solution is a little, well, clumsy. In the following trivial example I'm
only interested in finding the first matching list item:

>>> data = ['aaa','bbb','ccc','ddd','eee','fff','ggg','hhh'] foo =
>>> ['b','e','e']
>>> [data[[data.index(iy) for iy in data if iy.find(foo[ix]) > -1][0]] for
>>> ix in range(len(foo))]

['bbb', 'eee', 'eee']

So I guess this question is - have I missed a cleaner method then this
nested list comprehension?



Relevant Pages

  • Re: [].index
    ... ME> Recently I'd run into wishing a list's index method would match ... ME> substrings. ... Being fairly new to this game I can't help but think that my ...
    (comp.lang.python)
  • Re: [].index
    ... > Recently I'd run into wishing a list's index method would match ... > substrings. ... Being fairly new to this game I can't help but think that my ...
    (comp.lang.python)
  • Re: [].index
    ... > Recently I'd run into wishing a list's index method would match ... Being fairly new to this game I can't help but think that my ... a single character to the beginning of a string or will they sometimes be ...
    (comp.lang.python)