Find an element in a list of tuples

前端 未结 10 795
感动是毒
感动是毒 2020-11-27 10:58

I have a list \'a\'

a= [(1,2),(1,4),(3,5),(5,7)]

I need to find all the tuples for a particular number. say for 1 it will be



        
10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 11:45

    If you just want the first number to match you can do it like this:

    [item for item in a if item[0] == 1]
    

    If you are just searching for tuples with 1 in them:

    [item for item in a if 1 in item]
    

提交回复
热议问题