python将两个元组列表组合成一个大元组列表?(python combining two lists of tuples into one big list of tuples? [duplicat

编程入门 行业动态 更新时间:2024-10-28 00:24:17
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 answers

I'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)]

更多推荐

本文发布于:2023-08-07 16:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465488.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   列表   两个   大元组   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!