0

我想知道是否有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']
4

1 回答 1

1

我强烈建议您使用networkx 来处理 Graph。

import networkx as nx
G = nx.Graph()
# build your Graph
# G.add_node(), G.add_nodes_from(), G.add_edge(), G.add_edges_from()...

nodes = [(g, G.degree(g)) for g in G.nodes()]
# nodes like this: [(338, 4), (340, 7)...]
# item one is the node, and item two is the edges connected with this node

nodes.sort(key=lambda n: n[1], reverse=True)

# you wanna delete the third node and other nodes which edges at most 5, right?
G.remove_node(nodes[2][1])
for n, e in nodes:
    if e > 5:
        G.remove_node(n)

但是,上面只是您的代码,我将使其如下所示:

from collections import Counter

sources = []
for edge in edges:
    source.append(edge['source'])
    source.append(edge['target'])

sources_count = Counter(sources)
sources_count = sorted(source_count.items(), key=lambda s: s[1], reverse=True)

sources_count.pop(2)
valid_nodes = filter(lambda s: s[1] <= 5, sources_count)

link_list = filter(
    lambda e: e['source'] not in valid_nodes and e['target'] not in valid_nodes, 
    edges
)
于 2013-02-27T05:31:28.577 回答