Print an element in a list based on a condition

前端 未结 3 1710
难免孤独
难免孤独 2021-01-29 14:45

I have a list of lists which contain a first name, last name, and points scored.

list1 = [[\'david\', \'carter\', 6], [\'chris\', \'jenkins\', 0], [\'john\', \'w         


        
相关标签:
3条回答
  • 2021-01-29 15:23
    >>>[item for item in list1 if item[2]==0]
    
    ans:-
    [['chris', 'jenkins', 0], ['ryan', 'love', 0]]
    
    0 讨论(0)
  • 2021-01-29 15:31

    Keep it simple:

    for e in list1:
        if e[2] == 0:
            print e
    
    0 讨论(0)
  • 2021-01-29 15:41
    >>> from itertools import ifilterfalse
    >>> from operator import itemgetter
    >>> list(ifilterfalse(itemgetter(2), list1))
    [['chris', 'jenkins', 0], ['ryan', 'love', 0]]
    
    0 讨论(0)
提交回复
热议问题