Python 根据嵌套列表的概率删除嵌套列表中的项
所以我想在保留嵌套结构的同时删除嵌套列表中的重复项,删除哪个项是基于概率的决定。以下是我所拥有的:Python 根据嵌套列表的概率删除嵌套列表中的项,python,dictionary,multidimensional-array,nested-loops,nested-lists,Python,Dictionary,Multidimensional Array,Nested Loops,Nested Lists,所以我想在保留嵌套结构的同时删除嵌套列表中的重复项,删除哪个项是基于概率的决定。以下是我所拥有的: import collections new_sample = collections.defaultdict(list, {"dv1": [[3, 141], [13]], "dv2":[[3, 141], [157]], "dv3":[[0],[2]]}) new_sample_proba = collections.default
import collections
new_sample = collections.defaultdict(list, {"dv1": [[3, 141], [13]], "dv2":[[3, 141], [157]], "dv3":[[0],[2]]})
new_sample_proba = collections.defaultdict(list, {"dv1": [[0.8, 0.9], [0.9]], "dv2": [[0.9, 0.8], [0.9]], "dv3": [[0.9], [0.95]]})
print("before fix: ",new_sample)
print("before fix: ",new_sample_proba)
full_views_list = list(new_sample.keys())
temp_views_list = list(new_sample.keys())
for curr_view in full_views_list:
print("t1 ",new_sample[curr_view])
print("t1 ",new_sample_proba[curr_view])
temp_views_list.remove(curr_view)
for i, curr_class_new in enumerate(new_sample[curr_view]):
temp_pop_list = []
for j, class_item in enumerate(curr_class_new):
print(class_item)
for other_view in temp_views_list:
print("t2 org",new_sample[other_view])
print("t2 org",new_sample_proba[other_view])
for l, other_view_class_new in enumerate(new_sample[other_view]):
for m, other_class_item in enumerate(other_view_class_new):
# only perform delete if current view contain item, if it's deleted, move on
if new_sample[curr_view][i][j] == new_sample[other_view][l][m]:
if new_sample_proba[curr_view][i][j]>new_sample_proba[other_view][l][m]:
new_sample[other_view][l].pop(m)
new_sample_proba[other_view][l].pop(m)
print("t2 update",new_sample)
print("t2 update",new_sample_proba)
else:
temp_pop_list.append(j)
print(class_item, "check")
temp_pop_list.sort(reverse=True)
for idx in temp_pop_list:
new_sample[curr_view][i].pop(idx)
new_sample_proba[curr_view][i].pop(idx)
print("apply fix: ",new_sample)
print("apply fix: ",new_sample_proba)
因此,上面的代码是有效的,我如何缩短它?你能解释一下你的代码应该做什么吗?请提供3或4个示例的输入和预期输出(看看它们应该如何“冲突”,每个示例?只有“邻居”?)你能解释一下你的代码应该做什么吗?请提供3或4个示例的输入和预期输出(看看它们应该如何“冲突”,每个都是?只有“邻居”?…?)