Python删除反向矩阵中的重复情况
我有一个如下列表:Python删除反向矩阵中的重复情况,python,deduplication,Python,Deduplication,我有一个如下列表: relationShipArray = [] relationShipArray.append([340859419124453377, 340853571828469762]) relationShipArray.append([340859419124453377, 340854579195432961]) relationShipArray.append([340770796777660416, 340824159120654336]) relationShipArr
relationShipArray = []
relationShipArray.append([340859419124453377, 340853571828469762])
relationShipArray.append([340859419124453377, 340854579195432961])
relationShipArray.append([340770796777660416, 340824159120654336])
relationShipArray.append([340509588065513473, 340764841658703872])
relationShipArray.append([340478540048916480, 340671891540934656])
relationShipArray.append([340853571828469762, 340854579195432961])
relationShipArray.append([340842710057492480, 340825411573399553])
relationShipArray.append([340825411573399553, 340770796777660416])
relationShipArray.append([340825411573399553, 340824159120654336])
relationShipArray.append([340824159120654336, 340770796777660416])
relationShipArray.append([340804620295221249, 340825411573399553])
relationShipArray.append([340684236191313923, 340663388122279937])
relationShipArray.append([340663388122279937, 340684236191313923])
relationShipArray.append([340859507280318464, 340859419124453377])
relationShipArray.append([340859507280318464, 340853571828469762])
relationShipArray.append([340859507280318464, 340854579195432961])
relationShipArray.append([340854599697178624, 340845885439229952])
relationShipArray.append([340836561937641472, 340851694759972864])
relationShipArray.append([340854579195432961, 340853571828469762])
relationShipArray.append([340844519832580096, 340854599697178624])
relationShipArray.append([340814054610305024, 340748443670683648])
relationShipArray.append([340851694759972864, 340836561937641472])
relationShipArray.append([340748443670683648, 340814054610305024])
relationShipArray.append([340739498356912128, 340825992832638977])
正如您所看到的,有些案例是重复的。
e、 g
与相同(但相反)
从列表中删除重复项的最佳方法是什么(有一定的效率,但如果需要,可以不使用它)?因此,在这种情况下,我将保留
[34085571828469762,34085579195432961]
,但是删除[34085579195432961,34085571828469762]
,如果关系数组的顺序不重要:
result = {tuple(sorted(item)) for item in relationShipArray}
如果relationShipArray
的顺序不重要:
result = {tuple(sorted(item)) for item in relationShipArray}
如果您需要保留订单,请使用OrderedICT:
from collections import OrderedDict
>>> L = [[1, 2], [4, 5], [1,2], [2, 1]]
>>> [[x, y] for x, y in OrderedDict.fromkeys(frozenset(x) for x in L)]
[[1, 2], [4, 5]]
编辑1
如果顺序不重要,您可以选择一套:
>>> [[x, y] for x, y in set(frozenset(x) for x in L)]
[[1, 2], [4, 5]]
编辑2
一种更通用的解决方案,适用于不同长度的列表,不仅适用于两个元素:
[list(entry) for entry in set(frozenset(x) for x in L)]
[list(entry) for entry in OrderedDict.fromkeys(frozenset(x) for x in L)]
如果您需要保留订单,请使用OrderedICT:
from collections import OrderedDict
>>> L = [[1, 2], [4, 5], [1,2], [2, 1]]
>>> [[x, y] for x, y in OrderedDict.fromkeys(frozenset(x) for x in L)]
[[1, 2], [4, 5]]
编辑1
如果顺序不重要,您可以选择一套:
>>> [[x, y] for x, y in set(frozenset(x) for x in L)]
[[1, 2], [4, 5]]
编辑2
一种更通用的解决方案,适用于不同长度的列表,不仅适用于两个元素:
[list(entry) for entry in set(frozenset(x) for x in L)]
[list(entry) for entry in OrderedDict.fromkeys(frozenset(x) for x in L)]
一行解决方案
relationShipArray = []
relationShipArray.append([340859419124453377, 340853571828469762])
relationShipArray.append([340859419124453377, 340854579195432961])
relationShipArray.append([340770796777660416, 340824159120654336])
relationShipArray.append([340509588065513473, 340764841658703872])
relationShipArray.append([340478540048916480, 340671891540934656])
relationShipArray.append([340853571828469762, 340854579195432961])
relationShipArray.append([340842710057492480, 340825411573399553])
relationShipArray.append([340825411573399553, 340770796777660416])
relationShipArray.append([340825411573399553, 340824159120654336])
relationShipArray.append([340824159120654336, 340770796777660416])
relationShipArray.append([340804620295221249, 340825411573399553])
relationShipArray.append([340684236191313923, 340663388122279937])
relationShipArray.append([340663388122279937, 340684236191313923])
relationShipArray.append([340859507280318464, 340859419124453377])
relationShipArray.append([340859507280318464, 340853571828469762])
relationShipArray.append([340859507280318464, 340854579195432961])
relationShipArray.append([340854599697178624, 340845885439229952])
relationShipArray.append([340836561937641472, 340851694759972864])
relationShipArray.append([340854579195432961, 340853571828469762])
relationShipArray.append([340844519832580096, 340854599697178624])
relationShipArray.append([340814054610305024, 340748443670683648])
relationShipArray.append([340851694759972864, 340836561937641472])
relationShipArray.append([340748443670683648, 340814054610305024])
relationShipArray.append([340739498356912128, 340825992832638977])
使用relationShipArray
中的所有列表及其反向对等项创建一个数组。然后使用np.unique
import numpy as np
Y = list(np.unique(np.array(relationShipArray +
[X[::-1] for X in relationShipArray])))
一行解决方案
relationShipArray = []
relationShipArray.append([340859419124453377, 340853571828469762])
relationShipArray.append([340859419124453377, 340854579195432961])
relationShipArray.append([340770796777660416, 340824159120654336])
relationShipArray.append([340509588065513473, 340764841658703872])
relationShipArray.append([340478540048916480, 340671891540934656])
relationShipArray.append([340853571828469762, 340854579195432961])
relationShipArray.append([340842710057492480, 340825411573399553])
relationShipArray.append([340825411573399553, 340770796777660416])
relationShipArray.append([340825411573399553, 340824159120654336])
relationShipArray.append([340824159120654336, 340770796777660416])
relationShipArray.append([340804620295221249, 340825411573399553])
relationShipArray.append([340684236191313923, 340663388122279937])
relationShipArray.append([340663388122279937, 340684236191313923])
relationShipArray.append([340859507280318464, 340859419124453377])
relationShipArray.append([340859507280318464, 340853571828469762])
relationShipArray.append([340859507280318464, 340854579195432961])
relationShipArray.append([340854599697178624, 340845885439229952])
relationShipArray.append([340836561937641472, 340851694759972864])
relationShipArray.append([340854579195432961, 340853571828469762])
relationShipArray.append([340844519832580096, 340854599697178624])
relationShipArray.append([340814054610305024, 340748443670683648])
relationShipArray.append([340851694759972864, 340836561937641472])
relationShipArray.append([340748443670683648, 340814054610305024])
relationShipArray.append([340739498356912128, 340825992832638977])
使用relationShipArray
中的所有列表及其反向对等项创建一个数组。然后使用np.unique
import numpy as np
Y = list(np.unique(np.array(relationShipArray +
[X[::-1] for X in relationShipArray])))
顺序重要吗?(也就是说,你保留哪一个重要,如果你改变了一些没有副本的订单吗?)订单重要吗?(也就是说,你保留哪一个重要吗?如果你切换没有重复的部分的顺序?)它不应该删除[1,2],如果有[2,1],它应该删除它。如果我理解正确,他希望它们翻转,如果[1,2]
和[2,1]
反转,它就不起作用。那不是OP所要求的尽管。。。现在获取ValueError:要解压缩的值太多。我的清单上有更多的项目,但人们会认为这不重要。有什么想法吗?如果你只想要前两个,就试试frozenset(x[:2])
。如果有[1,2]就不应该删除它,如果有[2,1]就应该删除它。如果我理解正确,他想把它们翻过来。如果[1,2]
和[2,1]
被颠倒,它就不起作用了。那不是OP所要求的尽管。。。现在获取ValueError:要解压缩的值太多。我的清单上有更多的项目,但人们会认为这不重要。有什么想法吗?如果您只想要前两个,请尝试frozenset(x[:2])
。item=tuple(item)
应该做什么?它不会以任何方式改变relationShipArray
。@MikeMüller项是list
s,不可散列,使tuple
s能够进行set
。我知道这一点。但是检查循环后的relationShipArray
中的内容。分配给循环变量不会更改列表。顺便说一句,你根本不想要一个元组,因为OP想把反向项也当作重复项来对待。@MikeMüller噢,谢谢!我在赶时间!好的,我已经编辑了我的答案,关于颠倒的项目,item.sort()
可以处理它们。好的。这类人能做到这一点。一行代码怎么样?:result={tuple(sorted(item))for item in relationShipArray}
item=tuple(item)
应该做什么?它不会以任何方式改变relationShipArray
。@MikeMüller项是list
s,不可散列,使tuple
s能够进行set
。我知道这一点。但是检查循环后的relationShipArray
中的内容。分配给循环变量不会更改列表。顺便说一句,你根本不想要一个元组,因为OP想把反向项也当作重复项来对待。@MikeMüller噢,谢谢!我在赶时间!好的,我已经编辑了我的答案,关于颠倒的项目,item.sort()
可以处理它们。好的。这类人能做到这一点。一行程序怎么样?:result={tuple(sorted(item)),用于relationShipArray中的项}