我在python中编写这个公式。 Please see the picture 我使用以下代码。它总是会出错。
import math
import matplotlib
matplotlib.use('TkAgg')
from pylab import *
import cmath
import networkx as nx
import random as rd
import numpy as np
g=nx.karate_club_graph()
for i in g.nodes_iter():
g.node[i]['theta']=1
a=abs((cmath.exp( (g.node[i]['theta']*(1j)) for i in g.nodes_iter()))/g.number_of_nodes())
runfile('C:/Users/Bhawesh/Desktop/workingdirectory/bookpractice.py', wdir='C:/Users/Bhawesh/Desktop/workingdirectory')
Traceback (most recent call last):
File "<ipython-input-6-9fd718b8faeb>", line 1, in <module>
runfile('C:/Users/Bhawesh/Desktop/workingdirectory/bookpractice.py', wdir='C:/Users/Bhawesh/Desktop/workingdirectory')
File "C:\Users\Bhawesh\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Users\Bhawesh\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/Bhawesh/Desktop/workingdirectory/bookpractice.py", line 20, in <module>
a=abs((cmath.exp( (g.node[i]['theta']*(1j)) for i in g.nodes_iter()))/g.number_of_nodes())
TypeError: a float is required
答案 0 :(得分:0)
我认为其中一个问题是:
a=abs((cmath.exp( (g.node[i]['theta']*(1j)) for i in g.nodes_iter()))/g.number_of_nodes())
你有1j,这是一个文字而python不知道如何执行它。
根据您的评论,请使用:
sumValue = sum(cmath.exp(g.node[i]['theta'] * cmath.sqrt(-1)) for i in g.nodes_iter())
r = abs(sumValue/g.number_of_nodes())