python将两个元组列表组合成一个大元组列表?(python combining two lists of tuples into one big list of tuples? [duplicate])
这个问题在这里已有答案:
如何在Python中连接两个列表? 28个答案 Python成语链(flatten)无限可迭代的有限迭代? [重复] 2个答案我四处搜索,似乎没有人问过这个问题(或者至少我找不到它)。
我有两个元组列表,我想将它们加入到一个元组列表中。
first = [('a', 1), ('b',2)] second = [('c',3), ('d',4)]我试过追加,加入,拉链,但没有一个是正确的。 我想得到这个:
wanted = [('a', 1), ('b',2), ('c',3), ('d',4)]有一个简单的方法吗?
编辑:我觉得真的很蠢......当然这是我忘记尝试的一件事:(
This question already has an answer here:
How do I concatenate two lists in Python? 28 answers Python idiom to chain (flatten) an infinite iterable of finite iterables? [duplicate] 2 answersI've searched around and it seems like nobody has asked this question (or at least I can't find it).
I have two lists of tuples and I want to join them to one list of tuples.
first = [('a', 1), ('b',2)] second = [('c',3), ('d',4)]I have tried appending, joining, zipping, but none of those are quite right. I want to get this:
wanted = [('a', 1), ('b',2), ('c',3), ('d',4)]Is there a simple way to do this?
EDIT: I feel really stupid... of course its the one thing I forgot to try :(
最满意答案
你可以用+
>>> first = [('a', 1), ('b',2)] >>> second = [('c',3), ('d',4)] >>> first + second [('a', 1), ('b', 2), ('c', 3), ('d', 4)]You can use +
>>> first = [('a', 1), ('b',2)] >>> second = [('c',3), ('d',4)] >>> first + second [('a', 1), ('b', 2), ('c', 3), ('d', 4)]更多推荐
发布评论