首页 > 解决方案 > 在python中以不等间隔加入不同长度的列表?

问题描述

我正在尝试加入两个具有不同数据长度的列表。

list1 = [a,b,c,d,e,f]
list2 = [1,2,3,4,5,6,7,8,9,10,11,12]

我想要的输出是这样的:

[a12, b34, c56, d78, e910, f1112

我的第一个想法是在不同的时间间隔做一些像增量 i

for i in printlist:
    for i in stats:
        print(printlist[i] + stats[i] + stats[i+1])

但这会吐出来TypeError: list indices must be integers or slices, not str,我认为这不起作用,因为我会为两个列表错误地增加。

任何帮助,将不胜感激

标签: pythonlistappend

解决方案


尝试这个,

list1 = ["a","b","c","d","e","f"]
list2 = [1,2,3,4,5,6,7,8,9,10,11,12]

list3 = [str(i)+str(j) for i,j in zip(list2[::2], list2[1::2])]
["".join(list(x)) for x in zip(list1, list3)]

输出

['a12', 'b34', 'c56', 'd78', 'e910', 'f1112']

推荐阅读