如何在 Python 中合并两个元组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14745199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:18:40 来源:igfitidea点击:
How to merge two tuples in Python?
提问by Cory
How to convert the following tuple:
如何转换以下元组:
from:
从:
(('aa', 'bb', 'cc'), 'dd')
to:
到:
('aa', 'bb', 'cc', 'dd')
采纳答案by Volatility
l = (('aa', 'bb', 'cc'), 'dd')
l = l[0] + (l[1],)
This will work for your situation, however John La Rooy's solutionis better for general cases.
这将适用于您的情况,但是John La Rooy 的解决方案更适合一般情况。
回答by xvorsx
x = (('aa', 'bb', 'cc'), 'dd')
tuple(list(x[0]) + [x[1]])
回答by John La Rooy
>>> tuple(j for i in (('aa', 'bb', 'cc'), 'dd') for j in (i if isinstance(i, tuple) else (i,)))
('aa', 'bb', 'cc', 'dd')
回答by Thirumal Alagu
a = (1, 2)
b = (3, 4)
x = a + b
print(x)
Out:
出去:
(1, 2, 3, 4)

