so I have a list of tuple of the form (subject1,relationtype,sobject2)
, representing relational facts. I want to write a method that remove one of (subject1,relationtype,sobject2)
, (subject2,relationtype,sobject1)
if they both are in the list.
Here is what I tried:
def delete_symmetric_relations(A):
A = set(tuple(e) for e in A)
for (s,r,o) in A:
for (s1, r1, o1) in A:
if (s,r,o)==(o1,r1,s1) and (s,r,o) != (s1,r1,o1):
A.remove((s1,r1,o1))
return list(A)
print(delete_symmetric_relations(data))
I then get the error: RuntimeError: Set changed size during iteration
Example of how the method should work:
Say we have list [(1,in_same_numbersystem_as,3),(2,"is_smaller_than",4),(3,in_same_numbersystem_as,1),(2,"is_smaller_than",6)]
, the method should return one of [(2,"is_smaller_than",4),(3,in_same_numbersystem_as,1),(2,"is_smaller_than",6)]
or [(1,in_same_numbersystem_as,3),(2,"is_smaller_than",4),(2,"is_smaller_than",6)]
So from the suggestion, i rewrote the code as :
def delete_symmetric_relations(A):
somelist = [(s,r,o) for (s,r,o) in A if (o,r,s) not in A]
return somelist
But this code removes all (s,r,o) and (o,r,s) but I want to retain at least one.and got:
IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`
Because my list is very very large.
So how can i do it?