假设有一个元组列表,其中每个元组只能包含唯一的字符/数字,并且每个元组的顺序和长度是相同的。像这样的元组列表(1, 2, 3):l = [ (1 , 2, 3), #1 (None, 2, 3), #2 merge with #4 or #8 (1 , None, 3), #3 merge with #6 (1 , None, None), #4 overhead? (1 , 2, 3), #5 (None, 2, None), #6 (1 , 2, None), #7 overhead (1 , None, None), #8 overhead? ]但是在某些元组中,缺少值,并且应该将缺失的值与来自同一列表的匹配元组合并/补充。合并后,如果超过的任何元组仍包含None值,则应将其切除或附加在末尾。指定结果:l = [ (1 , 2, 3), #1 (1 , 2, 3), #2 merged with #4 (1 , 2, 3), #3 merged with #6 (1 , 2, 3), #4 ]python中有什么方法可以做到这一点吗?
1 回答
慕雪6442864
TA贡献1812条经验 获得超5个赞
尝试使用zip进行此嵌套列表理解:
list(zip(*[[f for f in e if f] for e in zip(*l)]))
输出:
[(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)]
添加回答
举报
0/150
提交
取消