在 django 的列表中创建一种特殊类型的合并列表

发布于 2024-12-12 10:37:50 字数 274 浏览 2 评论 0原文

我确信这很容易做到,但我不知道该怎么做。

我有三个列表:list1=[a1, a2, a3...]list2=[b1, b2, b3...]list3 =[c1, c2, c3...]

我想传递一个由这三个项目逐项合并的列表:

finalList = [[a1, b1, c1,], [a2, b2,c2], [a3, b3, c3]...]

我该怎么做?

I'm sure that this is pretty easy to do but I'm not sure how to do it.

I have three lists: list1=[a1, a2, a3...], list2=[b1, b2, b3...], and list3=[c1, c2, c3...]

I want to pass a list that is a by-item merger of these three:

finalList = [[a1, b1, c1,], [a2, b2, c2], [a3, b3, c3]...]

How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李白 2024-12-19 10:37:50

这就是内置的 zip() 的作用for:

final_list = zip(list1, list2, list3)

请注意,final_list实际上是一个元组列表,其长度将是最短输入列表的长度。

函数 itertools.izip()itertools.izip_longest() 也值得注意。

That's what the built-in zip() is for:

final_list = zip(list1, list2, list3)

Note that final_list will actually be a list of tuples, and its length will be the length of the shortest input list.

The functions itertools.izip() and itertools.izip_longest() are also noteworthy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文