我有一个像这样的列表列表:
[['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]项目的列表,并只允许其中一个。最好的方法是什么?
答案 0 :(得分:4)
一种方法是将它们添加到字典中,并将要过滤的项目作为键:
>>> 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']]