【问题标题】:Combining corresponding elements of lists in a nested list in Python [duplicate]在Python的嵌套列表中组合列表的相应元素[重复]
【发布时间】:2021-11-11 05:04:53
【问题描述】:

我有两个列表 A=[a,b,c,d] B=[1,2,3,4] 如何在 Python 中 A 和 B 的对应元素的嵌套列表中合并 A 和 B 我的结果将是 [[a,1],[b,2],[c,3],[d,4]]。 请帮我。 TIA

【问题讨论】:

    标签: python list nested-lists


    【解决方案1】:

    使用zip

    A = ['a', 'b', 'c', 'd']
    B = [1, 2, 3, 4]
    
    C = [list(x) for x in zip(A,B)]
    

    结果:

    [['a', 1], ['b', 2], ['c', 3], ['d', 4]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多