Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python—在一组列表中只保留唯一字段的最佳方法_Python_Performance_List - Fatal编程技术网

python—在一组列表中只保留唯一字段的最佳方法

python—在一组列表中只保留唯一字段的最佳方法,python,performance,list,Python,Performance,List,考虑这组列表: [ ["2" , "a" , "yes" , "next year"], ["2" , "b" , "yes" , "next year"], ["2" , "c" , "yes" , "next year"], ["2" , "a" , "yes" , "next 5 years"], ["2" , "a" , "yes" , "next year"], ["2" , "d" , "yes" , "next year"], ] 我希望获取这些列表并删除所有列表中相

考虑这组列表:

[
["2" , "a" , "yes" , "next year"], 
["2" , "b" , "yes" , "next year"], 
["2" , "c" , "yes" , "next year"], 
["2" , "a" , "yes" , "next 5 years"], 
["2" , "a" , "yes" , "next year"], 
["2" , "d" , "yes" , "next year"], 
]
我希望获取这些列表并删除所有列表中相同的所有“单元格”: 因此,对于上述数据,结果应为:

[
["a", "next year"], 
["b", "next year"], 
["c", "next year"], 
["a", "next 5  years"], 
["a", "next year"], 
["d", "next year"], 
]
删除了“2”和“是”的列,因为它们在所有行中都相同

我正在寻找一种有效的方法来做到这一点,因为我处理的项目非常多

另外,我想保留每个列表的顺序(我不关心外部列表中列表的顺序),您可以假设没有两行相同。

如果
len(set(I))>1
这意味着所有条目都不相等,因此您可以使用以下方法:

>>> map(list,zip(*[i for i in zip(*l) if len(set(i))>1]))
[['a', 'next year'], ['b', 'next year'], ['c', 'next year'], ['a', 'next 5 years'], ['a', 'next year'], ['d', 'next year']]

@GamesBrainic使用简单的方法,在第一行上迭代,然后在单元格上迭代,并检查其余的是否具有相同的值。如果是,删除单元格…为什么不起作用?@Tetsudou它起作用,但不是非常有效和可读,我相信有更好的方法来实现这一点,使用instance@Kasra这正是我需要的,谢谢