I have been trying to remove duplicate arrays in my array list (ie. [[2,1],[1,2]) I want to get rid of only one of the arrays. I tried to reverse the list to and remove the duplicates but that does not work.
def grf_to_edge_list(file):
edgelist = []
for line in file:
y = line.split()
for i in range(3,len(y)):
edgelist.append([int(y[0]),int(y[i])])
for i in range(len(edgelist)-1):
temp = edgelist[i]
temp.reverse()
if temp in edgelist:
edgelist.remove(temp)
i = i - 1
return edgelist
Here is the exact data:
1 2.0 1.0 2 3
2 1.0 0.0 1 3
3 0.0 2.0 1 2 4
4 3.0 3.0 3
5 3.0 0.0
2 Answers 2
You might as well not add them to the list in first place, if you want to remove them later stage.
def grf_to_edge_list(file):
edgelist = []
for line in file:
y = line.split()
for i in range(3,len(y)):
if [int(y[i]),int(y[0])] not in edgelist: #My change is here.
edgelist.append([int(y[0]),int(y[i])])
return edgelist
-
In the line
if [int(y[i]),int(y[0])] not in edgeList:
, theL
should be a smalll
. The code does not work otherwise.Nic3500– Nic35002020年10月07日 01:54:27 +00:00Commented Oct 7, 2020 at 1:54 -
Updated answer.Ganesh Jadhav– Ganesh Jadhav2020年10月07日 02:51:03 +00:00Commented Oct 7, 2020 at 2:51
def new_grf_to_edge_list(file):
edgelist = []
for line in file:
y = line.split()
for i in range(3,len(y)):
edgelist.append([int(y[0]),int(y[i])])
for i in range(len(edgelist)-1, 0, -1): # start from last item
temp = copy.deepcopy(edgelist[i]) # deepcopy so that reverse does not change original
temp.reverse()
if temp in edgelist:
edgelist.pop(i) # remove the item at i and not the one which is found
return edgelist
Please note the two changes. You cannot remove the temp because you are looping through the list. len(edgelist) changes. So if you loop from last-item you can remove it if it is found elsewhere since we are no longer going to access it(last-item in the loop).
[1, 2]
is not the same as[2, 1]
, but if you don't care about order and just want unique sets, you could just create sets instead of lists and put the sets in a set to remove duplicates among them.[0]
and the numbers after the third one as[1]
. This should have been explained in the question as well. The less we have to work to figure it out, the better your chances of getting answers.