我寻求一个普通的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'}
该功能必须:
[7,8]
不会变成[8,7]
)np.random.shuffle(magic_packing)
),则OK,但是不能是高级库方法(使用多重处理,编码等-应该是'普通')
答案 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)}]
zip
random
list
,但是tuple
效率更高x, y = ordered_shuffle(a, b)
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)))))]
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))]