Finding in elements in a tuple and filtering them

后端 未结 5 2063
借酒劲吻你
借酒劲吻你 2020-11-30 10:23

Assuming I have a tuple like:

[(\'text-1\',\'xxx\'), (\'img-1\',\'iii\'), (\'img-2\',\'jjj\'), (\'text-2\',\'xxx\')]

I want to filter out t

5条回答
  •  失恋的感觉
    2020-11-30 10:52

    Just another way with generator expression (it could make some difference on large lists)

    >>> l = [('text-1','xxx'), ('img-1','iii'), ('img-2','jjj'), ('text-2','xxx')]
    >>> (x for x in l if x[0].find('img') == 0)
    0:  at 0x917a00>
    >>> gen = (x for x in l if x[0].find('img') == 0)
    >>> list(gen)
    1: [('img-1', 'iii'), ('img-2', 'jjj')]
    

提交回复
热议问题