Python组合列表-保留相对顺序 [英] Python Combine Lists -- Retain Relative Ordering

查看:33
本文介绍了Python组合列表-保留相对顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定如何完成此任务...

Not sure how to get this guy done...

拥有:

L1 = [1,2,3]
L2 = [a,b,c]

想要:

[1,a,2,b,3,c]

推荐答案

import itertools

L1 = [1,2,3]
L2 = ['a','b','c']
list(itertools.chain.from_iterable(itertools.izip(L1, L2)))

您可以将 izip_longest 与填充值一起用于长度不均匀的列表.

You could use izip_longest with a fill value for lists of uneven length.

这篇关于Python组合列表-保留相对顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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