【问题标题】:how to concatenate two lists in python with numbers and strings?如何用数字和字符串连接 python 中的两个列表?
【发布时间】:2022-12-16 17:19:25
【问题描述】:

我想将两个列表合并在一起,但不是一个接一个地合并

list1=[1,2,3,4]
list2=[a,b,c,d,e,f]

输出应该是

list3=[1a,2b,3c,4d,e,f]

【问题讨论】:

  • 您需要将 list2 转换为 string format

标签: python python-3.x list concatenation


【解决方案1】:

利用:

from itertools import zip_longest

list1 = [1, 2, 3, 4]
list2 = ["a", "b", "c", "d", "e", "f"]

res = [f"{a}{b}" for a, b in zip_longest(list1, list2, fillvalue="")]
print(res)

输出

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 2017-11-10
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多