马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
求问为啥有如下差异,如何解决
运行第一个代码,能得到两张图,请关注第二张图,再运行第二个代码,会发现得到的图跟第一个代码的图不一样 import networkx as nx
import matplotlib.pyplot as plt
dictionary={ 3: [['O', 'H2'], ['H', 'OH']],36: [['H', 'O2'], ['HO2']], 38: [['H', 'O2'], ['O', 'OH']], 45: [['H', 'HO2'], ['O2', 'H2']]}
list=[[3,36,45],[3,45,38]]
for a in range(len(list)):
edgelist=[]
for b in list[a]:
fanyingwu=dictionary[b][0]
shengchengwu=dictionary[b][1]
for c in fanyingwu:
for d in shengchengwu:
edgelist.append((c,d))
G=nx.DiGraph()
G.add_edges_from(edgelist)
nx.draw_networkx(G, pos=None, arrows=True, with_labels=True)
plt.savefig('D:\\%s.png'%a)
第二个代码,仅是把list中的第一项删去了import networkx as nx
import matplotlib.pyplot as plt
dictionary={ 3: [['O', 'H2'], ['H', 'OH']],36: [['H', 'O2'], ['HO2']], 38: [['H', 'O2'], ['O', 'OH']], 45: [['H', 'HO2'], ['O2', 'H2']]}
list=[[3,45,38]]
for a in range(len(list)):
edgelist=[]
for b in list[a]:
fanyingwu=dictionary[b][0]
shengchengwu=dictionary[b][1]
for c in fanyingwu:
for d in shengchengwu:
edgelist.append((c,d))
G=nx.DiGraph()
G.add_edges_from(edgelist)
nx.draw_networkx(G, pos=None, arrows=True, with_labels=True)
plt.savefig('D:\\%s.png'%a)
第一个代码,最外层 for 循环需要循环 2 次
第二个代码,最外层 for 循环只需要循环 1 次
每次最外层 for 循环一次,最内层都要循环 3 次
导致你两个循环 edgelist.append((c,d)) 加入列表的数量不一致,所以产生的图片不同了
|