所以我有一个表格的元组列表(subject1,relationtype,sobject2),代表关系事实。如果它们都在列表中(subject1,relationtype,sobject2),我想编写一个删除其中一个的方法。(subject2,relationtype,sobject1)
这是我尝试过的:
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))
然后我得到错误: RuntimeError: Set changed size during iteration
该方法应该如何工作的示例:假设我们有 list [(1,in_same_numbersystem_as,3),(2,"is_smaller_than",4),(3,in_same_numbersystem_as,1),(2,"is_smaller_than",6)],该方法应该从建议中返回一个[(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)] ,我将代码重写为:
def delete_symmetric_relations(A):
somelist = [(s,r,o) for (s,r,o) in A if (o,r,s) not in A]
return somelist
但是这段代码删除了所有 (s,r,o) 和 (o,r,s) 但我想至少保留一个。得到:
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`
因为我的清单非常非常大。
那么我该怎么做呢?
森林海
眼眸繁星
繁星点点滴滴
随时随地看视频慕课网APP
相关分类