创建字典,其中值是转换为字符串的键
Create dictionary where values are keys transformed to strings
G
是一个图形对象,sorted(G)
returns 是一个节点列表:
[0,1,2...1860]
我需要重命名从字典映射的那些节点,如下所示:
mapping = {0: "0_cereb", 1: "1_cereb", 2: "2_cereb",..., 1860: "1860_cereb"}
然后我可以将 dict 应用于重新标记我的图表的方法:
import networkx as nx
Gnew = nx.relabel_nodes(G, mapping)
和sorted(Gnew)
returns:
[0_cereb, 2_cereb, ..., 1860_cereb]
如何创建此 mapping
dict
?
使用字典理解创建字典,并使用字符串格式化将数字转换为相应的字符串。
mapping = {x: f'{x}_cereb' for x in G}
答案是听写理解
mapping = {k: f"{k}_cereb" for k in sorted(G)}
将运行通过列表并创建键值对
G
是一个图形对象,sorted(G)
returns 是一个节点列表:
[0,1,2...1860]
我需要重命名从字典映射的那些节点,如下所示:
mapping = {0: "0_cereb", 1: "1_cereb", 2: "2_cereb",..., 1860: "1860_cereb"}
然后我可以将 dict 应用于重新标记我的图表的方法:
import networkx as nx
Gnew = nx.relabel_nodes(G, mapping)
和sorted(Gnew)
returns:
[0_cereb, 2_cereb, ..., 1860_cereb]
如何创建此 mapping
dict
?
使用字典理解创建字典,并使用字符串格式化将数字转换为相应的字符串。
mapping = {x: f'{x}_cereb' for x in G}
答案是听写理解
mapping = {k: f"{k}_cereb" for k in sorted(G)}
将运行通过列表并创建键值对