来自python元组

时间:2017-03-09 09:20:15

标签: python tuples networkx

在我的元组新下面:

如何将具有最高编号的“字符串”作为头节点,并将其他字符串名称附加到width =与其编号。

new = (('COCH', 8), ('CAB', 4), ('VSNL', 7), ('ZNRF', 8), ('SLC12A1', 4), 
('APC', 16), ('LOC', 8), ('TRPM', 4), ('TNFRSF', 22))

在上面的元组中,如何将最高数(22)“TNFRSF”作为节点,所有其他字符串都附加到该节点。连接宽度为各自的编号。例如,'COCH'连接到宽度= 8的“TNFRSF”节点。

1 个答案:

答案 0 :(得分:1)

import networkx as nx

new = (('COCH', 8), ('CAB', 4), ('VSNL', 7), ('ZNRF', 8), ('SLC12A1', 4), ('APC', 16), ('LOC', 8), ('TRPM', 4), ('TNFRSF', 22))
children = sorted(new, key=lambda x: x[1])
parent = children.pop()[0]

G = nx.Graph()
for child, weight in children: G.add_edge(parent, child, weight=weight)
width = list(nx.get_edge_attributes(G, 'weight').values())
nx.draw_networkx(G, width=width)