如何删除具有相同第二项[1]和第三项[2]的所有列表(python)
我有这样一个列表:如何删除具有相同第二项[1]和第三项[2]的所有列表(python),python,Python,我有这样一个列表: [['15 802', 'unicom.png', 'apple-iphone-6-16-gb'] ['15 805', 'unicom.png', 'apple-iphone-6-16-gb'] ['15 999', 'bomba.png', 'apple-iphone-6-16-gb'] ['15 979', 'bomba.png', 'apple-iphone-6-16-gb'] ['15 989', 'bomba.png', 'apple-iphone-6-1
[['15 802', 'unicom.png', 'apple-iphone-6-16-gb']
['15 805', 'unicom.png', 'apple-iphone-6-16-gb']
['15 999', 'bomba.png', 'apple-iphone-6-16-gb']
['15 979', 'bomba.png', 'apple-iphone-6-16-gb']
['15 989', 'bomba.png', 'apple-iphone-6-16-gb']
['9 299', 'netmarket.png', 'apple-iphone-5-64-gb']]
我尝试删除所有具有相同[1]和[2]项的列表,并仅允许其中一个。最好的方法是什么?一种方法是将它们添加到字典中,并将要筛选的项目作为关键字:
>>> l = [['15 802', 'unicom.png', 'apple-iphone-6-16-gb'],
['15 802', 'unicom.png', 'apple-iphone-6-16-gb'],
['15 999', 'bomba.png', 'apple-iphone-6-16-gb'],
['15 999', 'bomba.png', 'apple-iphone-6-16-gb'],
['15 999', 'bomba.png', 'apple-iphone-6-16-gb'],
['9 299', 'netmarket.png', 'apple-iphone-5-64-gb']]
>>> d = {}
>>> for item in l:
d[(item[0], item[1])] = item
>>> list(d.values())
[['15 802', 'unicom.png', 'apple-iphone-6-16-gb'], ['15 999', 'bomba.png', 'apple-iphone-6-16-gb'], ['9 299', 'netmarket.png', 'apple-iphone-5-64-gb']]
一种方法是将它们添加到字典中,并将要筛选的项作为键:
>>> l = [['15 802', 'unicom.png', 'apple-iphone-6-16-gb'],
['15 802', 'unicom.png', 'apple-iphone-6-16-gb'],
['15 999', 'bomba.png', 'apple-iphone-6-16-gb'],
['15 999', 'bomba.png', 'apple-iphone-6-16-gb'],
['15 999', 'bomba.png', 'apple-iphone-6-16-gb'],
['9 299', 'netmarket.png', 'apple-iphone-5-64-gb']]
>>> d = {}
>>> for item in l:
d[(item[0], item[1])] = item
>>> list(d.values())
[['15 802', 'unicom.png', 'apple-iphone-6-16-gb'], ['15 999', 'bomba.png', 'apple-iphone-6-16-gb'], ['9 299', 'netmarket.png', 'apple-iphone-5-64-gb']]
这些行是否总是像您展示的示例一样完全重复?如果不是,当第一项不同时,预期的结果是什么?第一个元素可以是不同的。这些行是否总是像您展示的示例那样完全重复?如果不是,当第一项不同时,预期的结果是什么?第一个元素可以是不同的。