Python: split a list based on a condition?

前端 未结 30 2550
误落风尘
误落风尘 2020-11-22 06:56

What\'s the best way, both aesthetically and from a performance perspective, to split a list of items into multiple lists based on a conditional? The equivalent of:

30条回答
  •  佛祖请我去吃肉
    2020-11-22 07:07

    Sometimes you won't need that other half of the list. For example:

    import sys
    from itertools import ifilter
    
    trustedPeople = sys.argv[1].split(',')
    newName = sys.argv[2]
    
    myFriends = ifilter(lambda x: x.startswith('Shi'), trustedPeople)
    
    print '%s is %smy friend.' % (newName, newName not in myFriends 'not ' or '')
    

提交回复
热议问题