これを行うためのpythonicな方法があるかどうか疑問に思っていました。
辞書のリストを考えてみましょう:
{'source': 338, 'target': 343, 'value': 0.667693}
{'source': 339, 'target': 342, 'value': 0.628195}
{'source': 340, 'target': 346, 'value': 0.529861}
{'source': 340, 'target': 342, 'value': 0.470139}
{'source': 341, 'target': 342, 'value': 0.762871}
{'source': 342, 'target': 349, 'value': 0.664869}
{'source': 343, 'target': 347, 'value': 0.513025}
{'source': 343, 'target': 344, 'value': 0.486975}
{'source': 344, 'target': 347, 'value': 0.536706}
{'source': 344, 'target': 349, 'value': 0.463294}
{'source': 345, 'target': 349, 'value': 0.546326}
{'source': 345, 'target': 346, 'value': 0.453674}
基本的に無向グラフです。しかし、非常に面倒です。少しきれいにしたい。
したがって、元の形式のようにエッジが最も多い上位 2 つのノードを残したいと思います。
残りのノードについては...最大5つのエッジが接続されています。
私はカウントで口述を維持しているだけです...それを逆に並べ替えます..
次に、上位2つを保存して、リストを再度調べます..エッジを削除しますが、上位2つをチェックします..
これを行うためのよりクリーンな方法はありますか。
私のバギー..厄介なサンプルコード:
import json
from pprint import pprint
import operator
json_data=open('topics350_1.json')
data = json.load(json_data)
edges = data["links"]
node_count_dict = {}
super_nodes = 3
min_nodes = 5
for edge in edges:
keys = [edge['source'], edge['target']]
for key in keys:
if key in node_count_dict:
node_count_dict[key] +=1
else:
node_count_dict[key] = 1
sorted_nodes = sorted(node_count_dict.iteritems(), key=operator.itemgetter(1),reverse = True)
#print sorted_nodes
top_nodes = sorted_nodes[super_nodes]
final_node_count = {}
for key in sorted_nodes:
final_node_count[key[0]] = 0
print final_node_count
link_list = []
for edge in edges:
keys = [edge['source'], edge['target']]
for key in keys:
if key not in top_nodes:
if final_node_count[key] < min_nodes:
link_list.append(edge)
print link_list
#print data['links']