Python: shuffling list, but keeping some elements frozen

后端 未结 5 721
隐瞒了意图╮
隐瞒了意图╮ 2021-01-08 00:25

I\'ve such a problem:

There is a list of elements of class CAnswer (no need to describe the class), and I need to shuffle it, but with one constraint -

5条回答
  •  灰色年华
    2021-01-08 00:46

    Use the fact that a list has fast remove and insert:

    • enumerate fixed elements and copy them and their index
    • delete fixed elements from list
    • shuffle remaining sub-set
    • put fixed elements back in

    See https://stackoverflow.com/a/25233037/3449962 for a more general solution.

    This will use in-place operations with memory overhead that depends on the number of fixed elements in the list. Linear in time. A possible implementation of shuffle_subset:

    #!/usr/bin/env python
    """Shuffle elements in a list, except for a sub-set of the elments.
    
    The sub-set are those elements that should retain their position in
    the list.  Some example usage:
    
    >>> from collections import namedtuple
    >>> class CAnswer(namedtuple("CAnswer","x fixed")):
    ...             def __bool__(self):
    ...                     return self.fixed is True
    ...             __nonzero__ = __bool__  # For Python 2. Called by bool in Py2.
    ...             def __repr__(self):
    ...                     return "".format(self.x)
    ...
    >>> val = [3, 2, 0, 1, 5, 9, 4]
    >>> fix = [2, 5]
    >>> lst = [ CAnswer(v, i in fix) for i, v in enumerate(val)]
    
    >>> print("Start   ", 0, ": ", lst)
    Start    0 :  [, , , , , , ]
    
    Using a predicate to filter.
    
    >>> for i in range(4):  # doctest: +NORMALIZE_WHITESPACE
    ...     shuffle_subset(lst, lambda x : x.fixed)
    ...     print([lst[i] for i in fix], end=" ")
    ...
    [, ] [, ] [, ] [, ]
    
    >>> for i in range(4):                # doctest: +NORMALIZE_WHITESPACE
    ...     shuffle_subset(lst)           # predicate = bool()
    ...     print([lst[i] for i in fix], end=" ")
    ...
    [, ] [, ] [, ] [, ]
    
    """
    from __future__ import print_function
    import random
    
    
    def shuffle_subset(lst, predicate=None):
        """All elements in lst, except a sub-set, are shuffled.
    
        The predicate defines the sub-set of elements in lst that should
        not be shuffled:
    
          + The predicate is a callable that returns True for fixed
          elements, predicate(element) --> True or False.
    
          + If the predicate is None extract those elements where
          bool(element) == True.
    
        """
        predicate = bool if predicate is None else predicate
        fixed_subset = [(i, e) for i, e in enumerate(lst) if predicate(e)]
    
        fixed_subset.reverse()      # Delete fixed elements from high index to low.
        for i, _ in fixed_subset:
            del lst[i]
    
        random.shuffle(lst)
    
        fixed_subset.reverse()      # Insert fixed elements from low index to high.
        for i, e in fixed_subset:
            lst.insert(i, e)
    
    if __name__ == "__main__":
        import doctest
        doctest.testmod()
    

提交回复
热议问题