如何以相同顺序随机播放多个可迭代对象?

时间:2019-10-01 02:09:12

标签: python random python-3.7 iterable

我寻求一个普通的Python函数,该函数接受任意数量的可迭代对象(元组,列表,字典),并以相同顺序将经过改组的返回它们

a = (1, 2, {3: 4}, 5)
b = [(5,6), [7,8], [9,0], [1,2]]
c = {'arrow': 5, 'knee': 'guard', 0: ('x',2)}

x, y, z = magic(a, b, c)
print(x, y, z, sep='\n')
# ({3: 4}, 1, 2)
# [[9, 0], (5, 6), [7, 8]]
# {0: ('x', 2), 'arrow': 5, 'knee': 'guard'}

该功能必须:

  1. 以相同顺序洗牌的返回可迭代项(请参见上文)
  2. 接受任意数量的可迭代对象
  3. 保留可迭代变量类型
  4. 支持任何 depth type
  5. 的嵌套可迭代对象
  6. 自身对嵌套元素进行随机播放(例如,上面的[7,8]不会变成[8,7]
  7. 返回迭代次数最短且没有上升错误的迭代变量(请参见上文)
如果在随机播放步骤中使用Numpy,random等(例如np.random.shuffle(magic_packing)),则

OK,但是不能是高级库方法(使用多重处理,编码等-应该是'普通')


我见过related SO's,但无法使它们适应这样的一般情况。如何完成?

3 个答案:

答案 0 :(得分:4)

这是一种基本方法:

import random
def shuffle_containers(*args):
    min_length = min(map(len, args))
    idx = list(range(min_length))
    random.shuffle(idx)
    results = []
    for arg in args:
        if isinstance(arg, list):
            results.append([arg[i] for i in idx])
        elif isinstance(arg, tuple):
            results.append(tuple(arg[i] for i in idx))
        elif isinstance(arg, dict):
            items = list(arg.items())
            results.append(dict(items[i] for i in idx))
        else:
            raise ValueError(
                "Encountered", type(arg),
                "expecting only list, dict, or tuple"
            )
    return results
a = (1, 2, {3: 4}, 5)
b = [(5,6), [7,8], [9,0], [1,2]]
c = {'arrow': 5, 'knee': 'guard', 0: ('x',2)}
x, y, z = shuffle_containers(a, b, c)
print(x, y, z, sep='\n')

请注意,这将忽略通过最小容器长度的所有项目,如果您不希望这样做,则将需要更复杂的逻辑。

编辑:

这里是两行代码:

def shuffle_containers(*args):
    min_length = min(map(len, args)); idx = list(range(min_length)); random.shuffle(idx)
    return [ [arg[i] for i in idx] if isinstance(arg, list) else tuple(arg[i] for i in idx) if isinstance(arg, tuple) else dict(list(args.items())[i] for i in idx) ]

当然,上面的内容可读性差,效率低且简单。不要做那样的事情。

答案 1 :(得分:2)

import random

a = (1, 2, {3: 4}, 5)
b = [(5,6), [7,8], [9,0], [1,2]]
c = {'arrow': 5, 'knee': 'guard', 0: ('x',2)}

def magic(*x):
    out = []
    # 6. length of shortest iterable
    min_len = min(len(a_org) for a_org in x)
    for a_org in x:
        if isinstance(a_org, list) or isinstance(a_org, tuple):
            indices = list(range(len(a_org)))
            random.shuffle(indices)
            a_copy = type(a_org)(a_org[i] for i in indices[:min_len])
        elif isinstance(a_org, dict):
            indices = list(a_org.keys())
            random.shuffle(indices)
            a_copy = {i:a_org[i] for i in indices[:min_len]}
        else:
            raise "not supported type"

        out.append(a_copy)
    return tuple(out)

print(magic(a, b, c))

答案 2 :(得分:-2)

def ordered_shuffle(*args):
    args_types = [type(arg) for arg in args]                               # [1]
    _args      = [arg if type(arg)!=dict else arg.items() for arg in args] # [2]
    args_split = [arg for arg in zip(*_args)]                              # [3]
    args_shuffled = random.sample(args_split, len(args_split))             # [4]
    args_shuffled = map(tuple, zip(*args_shuffled))                        # [5]
    return [args_types[i](arg) for i, arg in enumerate(args_shuffled)]     # [6]

EXPLANATION : 分一个简单的例子:

a = [1, 2, 3]
b = ([1,2], [3,4], [5,6])
c = {'a': 1, 'b': 2, 'c': 3}

# [1]: list, tuple, dict
# [2]: [[1, 2, 3],
#       ([1, 2], [3, 4], [5, 6]),
#       dict_items([('a', 1), ('b', 2), ('c', 3)])]
# [3]: [(1, [1, 2], ('a', 1)), 
#       (2, [3, 4], ('b', 2)), 
#       (3, [5, 6], ('c', 3))]
# [4]: [(1, [1, 2], ('a', 1)), 
#       (3, [5, 6], ('c', 3)), 
#       (2, [3, 4], ('b', 2))]
# [5]: (1, 2, 3)
#      ([1, 2], [3, 4], [5, 6])
#      (('a', 1), ('b', 2), ('c', 3))
# [6]: [(1, 2, {3: 4}),
#       [(5, 6), [7, 8], [9, 0]],
#       {'arrow': 5, 'knee': 'guard', 0: ('x', 2)}]
  1. 存储原始类型以供以后还原
  2. 默认情况下,Python仅在字典键上进行迭代-我们也需要值
  3. Python词典不能直接改组-而是先将键值对转换为 tuples 来间接改组;所有可迭代对象也应同时迭代 进行重组,以完成{/ zip
  4. 使用Python的本机random
  5. 也可以转换为list,但是tuple效率更高
  6. 还原原始类型并以元组形式返回,以将其解压缩为x, y = ordered_shuffle(a, b)


Lambda解决方案:(积分:Gloweye

ordered_shuffle = lambda *args:[type(args[i])(arg) for i, arg in enumerate(map(tuple, 
            zip(*random.sample([y for y in zip(*[x if type(x)!=dict else x.items() 
            for x in args])], min(len(z) for z in args)))))]


最快捷的解决方案 :(信用:GZ0)(缩短变量名以使其最短)

def ordered_shuffle(*args):
    zipped_args = list(zip(*(a.items() if isinstance(a, dict) else a for a in args)))
    random.shuffle(zipped_args)
    return [cls(elements) for cls, elements in zip(map(type, args), zip(*zipped_args))]