[英]How to get unique combinations of pairs in nested tuples?
我有一个 [(tuple1), (tuple2)] 形式的对列表,其中第一个元组的长度可变,第二个元组的长度为 1。
例子:
[((0, 1, 2), 0),
((3, 4, 5), 0),
((12,), 1),
((0, 1, 4, 7), 1),
((12,), 1),
((3, 4, 5), 0)]
我想要set(pairs)
,其中tuple1_pair_X = tuple1_pair_Y && tuple2_pair_X = tuple2_pair_Y
而不是 tuple1 元素与 tuple2 的所有可能组合。
期望输出:
[((0, 1, 2), 0),
((3, 4, 5), 0),
((12,), 1),
((0, 1, 4, 7), 1)]
我想只需在元组的原始列表上使用 set() 命令就可以工作并提供所需的输出。
tup_list = [((0, 1, 2), 0), ((3, 4, 5), 0),((12,), 1),((0, 1, 4, 7), 1),((12,), 1),((3, 4, 5), 0)]
output = list(set(tup_list))
如果您想保持列表的顺序:
lst = [((0, 1, 2), 0),
((3, 4, 5), 0),
((12,), 1),
((0, 1, 4, 7), 1),
((12,), 1),
((3, 4, 5), 0)]
sorted(set(lst), key=lst.index)
输出:
[((0, 1, 2), 0),
((3, 4, 5), 0),
((12,), 1),
((0, 1, 4, 7), 1)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.