如何在Python中合并两个元组?

How to merge two tuples in Python?

如何转换以下元组:

来自:

1
(('aa', 'bb', 'cc'), 'dd')

到:

1
('aa', 'bb', 'cc', 'dd')


1
2
l = (('aa', 'bb', 'cc'), 'dd')
l = l[0] + (l[1],)

这将是你的工作情况,但最好的解决方案是一gnibbler在一般的情况下。


1
2
3
4
5
6
a = (1, 2)
b = (3, 4)

x = a + b

print(x)

10:

1
(1, 2, 3, 4)


1
2
>>> tuple(j for i in (('aa', 'bb', 'cc'), 'dd') for j in (i if isinstance(i, tuple) else (i,)))
('aa', 'bb', 'cc', 'dd')

1
2
x = (('aa', 'bb', 'cc'), 'dd')
tuple(list(x[0]) + [x[1]])