Finding in elements in a tuple and filtering them

后端 未结 5 2070
借酒劲吻你
借酒劲吻你 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 11:10

    With Normal Loop

    input = [('text-1','xxx'), ('img-1','iii'), ('img-2','jjj'), ('text-2','xxx')]
    inp = []
    for i in input:
        if 'img' in i[0]:
            inp.append(i)
    print(inp)
    

    or by Using lambda function

    inp = list(filter(lambda x: 'img' in x[0],input))
     print(inp)
    

    or by Using List Comprehension

     inp = [i for i in input if 'img' in i[0]]
     print(inp)
    

提交回复
热议问题