【问题标题】:how I can iterate through two different lists and make a new list, which has lists as elements , which elements have what is inside the two lists [duplicate]我如何遍历两个不同的列表并创建一个新列表,该列表将列表作为元素,哪些元素具有两个列表中的内容[重复]
【发布时间】:2020-01-27 10:21:37
【问题描述】:

我想加入两个列表,使元素变成列表

输入:

a = [1,2,3,4,5,6,7]
b = [a,b,c,d,e,f,g]

输出:

c = [[1,a],[2,b],[3,c],[4,d],[5,e],[6,f],[7,g]]

到目前为止,我尝试这样做:

product = []
price = []

p = []
for g, h in zip(product, price):
    p.append([])
    for l in range(0, len(p)):
        p[l].append(g)
        p[l].append(h)
print(p)

【问题讨论】:

    标签: python-3.x list iteration


    【解决方案1】:

    此代码按预期工作

    a = [1, 2, 3, 4, 5, 6, 7]
    b = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    ab = zip(a, b)
    lis_com = [list(i) for i in ab]
    print (lis_com)
    

    输出:-

    [[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [5, 'e'], [6, 'f'], [7, 'g']]

    【讨论】:

      猜你喜欢
      • 2018-11-27
      • 2021-12-15
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2013-04-08
      相关资源
      最近更新 更多