Python: split a list based on a condition?

前端 未结 30 1872
误落风尘
误落风尘 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:23
    def partition(pred, iterable):
        'Use a predicate to partition entries into false entries and true entries'
        # partition(is_odd, range(10)) --> 0 2 4 6 8   and  1 3 5 7 9
        t1, t2 = tee(iterable)
        return filterfalse(pred, t1), filter(pred, t2)
    

    Check this

    0 讨论(0)
  • 2020-11-22 07:24

    Already quite a few solutions here, but yet another way of doing that would be -

    anims = []
    images = [f for f in files if (lambda t: True if f[2].lower() in IMAGE_TYPES else anims.append(t) and False)(f)]
    

    Iterates over the list only once, and looks a bit more pythonic and hence readable to me.

    >>> files = [ ('file1.jpg', 33L, '.jpg'), ('file2.avi', 999L, '.avi'), ('file1.bmp', 33L, '.bmp')]
    >>> IMAGE_TYPES = ('.jpg','.jpeg','.gif','.bmp','.png')
    >>> anims = []
    >>> images = [f for f in files if (lambda t: True if f[2].lower() in IMAGE_TYPES else anims.append(t) and False)(f)]
    >>> print '\n'.join([str(anims), str(images)])
    [('file2.avi', 999L, '.avi')]
    [('file1.jpg', 33L, '.jpg'), ('file1.bmp', 33L, '.bmp')]
    >>>
    
    0 讨论(0)
  • 2020-11-22 07:25
    good, bad = [], []
    for x in mylist:
        (bad, good)[x in goodvals].append(x)
    
    0 讨论(0)
  • 2020-11-22 07:25

    itertools.groupby almost does what you want, except it requires the items to be sorted to ensure that you get a single contiguous range, so you need to sort by your key first (otherwise you'll get multiple interleaved groups for each type). eg.

    def is_good(f):
        return f[2].lower() in IMAGE_TYPES
    
    files = [ ('file1.jpg', 33L, '.jpg'), ('file2.avi', 999L, '.avi'), ('file3.gif', 123L, '.gif')]
    
    for key, group in itertools.groupby(sorted(files, key=is_good), key=is_good):
        print key, list(group)
    

    gives:

    False [('file2.avi', 999L, '.avi')]
    True [('file1.jpg', 33L, '.jpg'), ('file3.gif', 123L, '.gif')]
    

    Similar to the other solutions, the key func can be defined to divide into any number of groups you want.

    0 讨论(0)
  • 2020-11-22 07:25

    I'd take a 2-pass approach, separating evaluation of the predicate from filtering the list:

    def partition(pred, iterable):
        xs = list(zip(map(pred, iterable), iterable))
        return [x[1] for x in xs if x[0]], [x[1] for x in xs if not x[0]]
    

    What's nice about this, performance-wise (in addition to evaluating pred only once on each member of iterable), is that it moves a lot of logic out of the interpreter and into highly-optimized iteration and mapping code. This can speed up iteration over long iterables, as described in this answer.

    Expressivity-wise, it takes advantage of expressive idioms like comprehensions and mapping.

    0 讨论(0)
  • 2020-11-22 07:26

    For perfomance, try itertools.

    The itertools module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.

    See itertools.ifilter or imap.

    itertools.ifilter(predicate, iterable)

    Make an iterator that filters elements from iterable returning only those for which the predicate is True

    0 讨论(0)
提交回复
热议问题