如何从python中的两个列表生成嵌套列表 [英] how to produce a nested list from two lists in python

查看:1563
本文介绍了如何从python中的两个列表生成嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手, 我有两个列表:

I am a newbie in python, I have two lists:

l1 = ['a','b','c','d']
l2 = ['new']

我想要这样的新列表

l3 = [('a','new'),('b','new'),('c','new'),('d','new')]

结合两个列表的最佳方法是什么?

What is the best way to combine the two lists?

推荐答案

>>> from itertools import product
>>> l1 = ['a','b','c','d']
>>> l2 = ['new']
>>> list(product(l1,l2))
[('a', 'new'), ('b', 'new'), ('c', 'new'), ('d', 'new')]

这篇关于如何从python中的两个列表生成嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆