合集传送门:代码片段精选
将两个或多个列表合并为一个列表的列表,根据每个输入列表的位置合并元素。
def merge(args, fill_value = None): max_length = max([len(lst) for lst in args]) result = []for i in range(max_length): result.append([ args[k][i] if i < len(args[k]) else fill_value for k in range(len(args)) ])return resultmerge(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]]merge(['a'], [1, 2], [True, False]) # [['a', 1, True], [None, 2, False]]merge(['a'], [1, 2], [True, False], fill_value = '_')# [['a', 1, True], ['_', 2, False]]
Source: https://www.30secondsofcode.org/python/s/merge/
(图片来自网络侵删)
- EOF -

告诉你更多细节干货
欢迎围观我的朋友圈
?每天更新所想所悟