Python 如何从嵌套列表中删除重复元素?
这是嵌套列表Python 如何从嵌套列表中删除重复元素?,python,Python,这是嵌套列表 [['Dell', 2], ['Dell', 2], ['HP', 1], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Apple', 1], ['Sony', 5], ['Apple', 1]] 输出应为 [['Dell', 2], ['HP', 1], ['Sony', 5], ['Apple', 1],] 只需遍历您的列表并添加到其他列表(如果尚未添加) data = [['Dell', 2], ['Dell
[['Dell', 2], ['Dell', 2], ['HP', 1], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Apple', 1], ['Sony', 5], ['Apple', 1]]
输出应为
[['Dell', 2], ['HP', 1], ['Sony', 5], ['Apple', 1],]
只需遍历您的列表并添加到其他列表(如果尚未添加)
data = [['Dell', 2], ['Dell', 2], ['HP', 1], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Apple', 1], ['Sony', 5], ['Apple', 1]]
new_data = []
for i in data:
if i not in new_data:
new_data.append(i)
print(new_data)
# Output [['Dell', 2], ['HP', 1], ['Sony', 5], ['Apple', 1]]
如果订单不重要:
lis = [['Dell', 2], ['Dell', 2], ['HP', 1], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Apple', 1], ['Sony', 5], ['Apple', 1]]
result = list(map(list, set(map(tuple, lis))))
print(result)
如果希望嵌套项是列表而不是元组
remove_duplicates = [list(y) for y in list(set(tuple(x) for x in before_remove))]
你试过什么?显示你的努力这是否回答了你的问题?
remove_duplicates = [list(y) for y in list(set(tuple(x) for x in before_remove))]