Select value from list of tuples where condition

前端 未结 3 1824
旧时难觅i
旧时难觅i 2020-12-04 17:48

I have a list of tuples. Every tuple has 5 elements (corresponding to 5 database columns) and I\'d like to make a query

select attribute1 from mylist where a         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 18:14

    One solution to this would be a list comprehension, with pattern matching inside your tuple:

    >>> mylist = [(25,7),(26,9),(55,10)]
    >>> [age for (age,person_id) in mylist if person_id == 10]
    [55]
    

    Another way would be using map and filter:

    >>> map( lambda (age,_): age, filter( lambda (_,person_id): person_id == 10, mylist) )
    [55]
    

提交回复
热议问题