1

鉴于:

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)]但我不完全理解正确的语法和逻辑。

谢谢你。

4

4 回答 4

5

似乎您想要的东西可以像setB在列表理解中的字典查找一样容易地实现。

mapping = dict(setB)
out = [(b, a, mapping.get(a)) for a, b in setA]

print(out)
[(25, 1, 19),
 (24, 2, 18),
 (23, 3, 17),
 (22, 4, 16),
 (21, 5, 15),
 (20, 6, 14),
 (19, 7, 13),
 (18, 8, 12),
 (17, 9, 11),
 (16, 10, 10),
 (15, 11, 9),
 (14, 12, 8),
 (13, 13, 7),
 (12, 14, 6),
 (11, 15, 5),
 (10, 16, 4),
 (9, 17, 3),
 (8, 18, 2),
 (7, 19, 1),
 (6, 20, None),
 (5, 21, None),
 (4, 22, None),
 (3, 23, None),
 (2, 24, None),
 (1, 25, None)]
于 2018-05-10T06:02:11.093 回答
2

由于我们的列表有不同的大小zip不是解决方案。

一种解决方案可能是使用包中的zip_longest方法itertools

finalSet = [(b, a, c[1] if c is not None else c) for (a,b), c in zip_longest(*setA,*setB)]

输出

(25, 1, 19)
(24, 2, 18)
(23, 3, 17)
(22, 4, 16)
(21, 5, 15)
(20, 6, 14)
(19, 7, 13)
(18, 8, 12)
(17, 9, 11)
(16, 10, 10)
(15, 11, 9)
(14, 12, 8)
(13, 13, 7)
(12, 14, 6)
(11, 15, 5)
(10, 16, 4)
(9, 17, 3)
(8, 18, 2)
(7, 19, 1)
(6, 20, None)
(5, 21, None)
(4, 22, None)
(3, 23, None)
(2, 24, None)
(1, 25, None)
于 2018-05-10T06:08:28.257 回答
1
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)]
la, lb = len(setA), len(setB)
temp=[[setA[i][1] if i<la else None, i+1, setB[i][1] if i<lb else None] for i in range(0,max(la,lb))]

[[25, 1, 19],
 [24, 2, 18],
 [23, 3, 17],
 [22, 4, 16],
 [21, 5, 15],
 [20, 6, 14],
 [19, 7, 13],
 [18, 8, 12],
 [17, 9, 11],
 [16, 10, 10],
 [15, 11, 9],
 [14, 12, 8],
 [13, 13, 7],
 [12, 14, 6],
 [11, 15, 5],
 [10, 16, 4],
 [9, 17, 3],
 [8, 18, 2],
 [7, 19, 1],
 [6, 20, None],
 [5, 21, None],
 [4, 22, None],
 [3, 23, None],
 [2, 24, None],
 [1, 25, None]]
于 2018-05-10T06:01:32.790 回答
0

如果您希望 setC 的格式与 setA 和 setB 相同。我认为这种解决方法可以。直接作为元组输入值是不可能的,因为元组是不可变的,因此我们将新元组附加为列表,然后将其转换为元组。`

setC = []
i=0
while setA[0][i][0]== setB[0][i][0] and (i < min(len(setA[0]), len(setB[0]))-1):
    setC.append((setA[0][i][1],setA[0][i][0], setB[0][i][1]))
    i+=1
setC = [tuple(setC)]

`

于 2018-05-10T06:19:47.110 回答