鉴于:
setA = [(1, 25), (2, 24), (3, 23), (4, 22), (5, 21), (6, 20),
(7, 19), (8, 18), (9, 17), (10, 16), (11, 15), (12, 14),
(13, 13),(14, 12), (15, 11), (16, 10), (17, 9), (18, 8),
(19, 7),(20, 6), (21, 5), (22, 4), (23, 3), (24, 2), (25, 1)]
setB = [(1, 19), (2, 18), (3, 17), (4, 16), (5, 15), (6, 14), (7, 13),
(8, 12), (9, 11), (10, 10), (11, 9), (12, 8), (13, 7), (14, 6),
(15, 5), (16, 4), (17, 3), (18, 2), (19, 1)]
如何使用每个集合中每个元组的第一个元素作为公共键值来组合这两个集合。因此,对于每个集合中位置 1 的元组,它将分别为 (1,25) 和 (1,19)。结合在一起将产生:(25,1,19)
(25,1,19)
(24,2,18)
(23,3,17)
...
(7,19,1)
(6,20,none)
...
(2,24,none)
(1,25,none)
注意:必须保持输出元组的顺序。例子:
(setA value, common value, setB value)
(setA value, common value, setB value)etc...
注意:必须使用 Python 2.7x 标准库
我正在尝试做类似的事情,[(a,b,c) for (a,b),(b,c) in zip(setA,setB)]
但我不完全理解正确的语法和逻辑。
谢谢你。