如何使用python从arraylist中删除重复项?
问题1: 我有一个数组列表如何使用python从arraylist中删除重复项?,python,Python,问题1: 我有一个数组列表 x= [[1,2,-1],[1,-1,0],[-1,0,1]] 最后我想得到x=[[1,2,-1],[1,-1,0],因为[1,-1,0]和[-1,0,1]是相同的,但顺序不同 问题2: 为了 同样的想法,我想得到temp=[[0,0,0]],这意味着像Q1一样删除arraylist中的所有其他副本 我的代码不起作用。它说列表索引超出范围,但我使用temp2来len(temp1)更改……为什么 temp1 = [[0,0,0],[0,0,0],[0,0,0],[0,
x= [[1,2,-1],[1,-1,0],[-1,0,1]]
最后我想得到x=[[1,2,-1],[1,-1,0]
,因为[1,-1,0]
和[-1,0,1]
是相同的,但顺序不同
问题2:
为了
同样的想法,我想得到temp=[[0,0,0]]
,这意味着像Q1一样删除arraylist中的所有其他副本
我的代码不起作用。它说列表索引超出范围,但我使用temp2
来len(temp1)
更改……为什么
temp1 = [[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
temp2 = temp1
for i in range(0, len(temp1)):
for j in range(i+1, len(temp1)):
if(set(temp1[i]) == set(temp1[j])):
temp2.remove(temp2[i])
您不应该更改正在迭代的列表!而且
temp2=temp1
不会复制。之后,您只有两个名称引用同一列表。如果要制作(浅层)副本,可以使用temp2=temp1.copy()
或temp2=temp1[:]
或temp2=list(temp1)
一般注意事项:使用两次迭代将具有二次运行时行为,将已处理的项保留在集合中会更快,该集合具有O(1)
查找(大多数情况下):
如果您可以并且希望使用第三方软件包,我有一个包含迭代器的软件包,它可以实现以下功能:
为了删除DUP,我们可以首先对列表进行排序:
lsts = [[1,2,-1],[1,-1,0],[-1,0,1]]
lsts = [sorted(x) for x in lsts]
然后将列表转换为元组,并将它们添加到一个集合中,以消除重复(我们无法将列表添加到集合中,因为它们不可散列,所以我们必须首先将它们转换为元组):
然后我们可以将元组和集合转换回列表:
lsts = list(list(x) for x in res)
print(lsts) # [[-1, 1, 2], [-1, 0, 1]]
失败的原因是您正在修改正在迭代的列表,因此通过删除项目,您可以缩短列表,然后尝试访问不再存在的索引,但您可以通过迭代列表而不使用索引来修复它:
temp1 = [[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
for x in temp1:
temp2 = temp1[:] # create a real copy of temp1
temp2.remove(x) # remove x so we won't consider it as dup of itself
for y in temp2:
if set(x) == set(y):
temp1.remove(x)
print(temp1) # [[0, 0, 0]]
一套可以:
lsts = [[1,2,-1],[1,-1,0],[-1,0,1]]
result = {tuple(sorted(x)) for x in lsts}
问题1。如果想在包含相同元素的情况下考虑列表相等,那么这样做的一种方法是在比较之前对它们进行排序,例如:
def return_unique(list_of_lists):
unique = []
already_added = set()
for item in list_of_lists:
# Convert to tuple, because lists are not hashable.
# We consider two things to be the same regardless of the order
# so before converting to tuple, we also sort the list.
# This way [1, -1, 0] and [-1, 0, 1] both become (-1, 0, 1)
sorted_tuple = tuple(sorted(item))
# Check if we've already seen this tuple.
# If we haven't seen it yet, add the original list (in its
# original order) to the list of unique items
if sorted_tuple not in already_added:
already_added.add(sorted_tuple)
unique.append(item)
return unique
temp1 = [[1, 2, -1], [1, -1, 0], [-1, 0, 1]]
temp2 = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
print(return_unique(temp1))
print(return_unique(temp2))
问题2。仅仅分配temp2=temp1
不会创建新的独立副本——它们仍然引用相同的列表。在这种情况下,可以使用copy.deepcopy
创建独立副本:
import copy
temp2 = copy.deepcopy(temp1)
这适用于第二季度
temp1 = [[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
temp2 = []
for element in temp1:
if element not in temp2:
temp2.append(element)
temp2
>>>[[0, 0, 0]]
您可以使用groupby
from itertools import groupby
[i for i,k in groupby(x, lambda j:sorted(j))]
输出:
[[-1, 1, 2], [-1, 0, 1]]
temp2仍然引用temp1所引用的相同列表。它不是一个不同的副本。您的意思是除了类型错误:不可损坏的类型:'list'
?:)@MSeifert,真的。你需要tuple(排序(x))
notsorted(tuple(x))
:)@MSeifert,同样是真的,不是我的一天,因为“重复项”彼此相邻,所以正好起作用。如果他们不是,那就不行了。不过,它在给定的示例中确实有效。字母应该是元素
import copy
temp2 = copy.deepcopy(temp1)
temp1 = [[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
temp2 = []
for element in temp1:
if element not in temp2:
temp2.append(element)
temp2
>>>[[0, 0, 0]]
from itertools import groupby
[i for i,k in groupby(x, lambda j:sorted(j))]
[[-1, 1, 2], [-1, 0, 1]]