我已经创建了一个程序,它给了我一个列表里面的列表,然后我想打印第一个列表的第一个元素但是当我这样做时,我有一个错误告诉我我的列表没有& #39; t存在......
我的节目是:
def Prog1():
f=open("dimer.out","r" )
m=open("dimer3.out","w")
g=f.readlines()
for k in range(len(g)):
if "Molecular Orbital" in g[k]:
c=k
for n in range(c+1, len(g)):
m.write(g[n])
if "Density Matrix" in g[n+1]:
break
def ReadMatrix(numberOfRows):
Ci = [];
for i in range(0,numberOfRows):
Ci.append([]);
f=open("dimer3.out","r")
lines=f.readlines()
numberOfBlocks = math.ceil(numberOfRows / 5)
for i in range(0,int(numberOfBlocks)):
for j in range(i*(numberOfRows+3)+3, i*(numberOfRows+3)+3+numberOfRows):
array = re.split('\s+', lines[j])
for k in range(0,len(array)):
if re.search("\.",array[k]) is not None:
Ci[j - 3 - i*(numberOfRows+3)].append(array[k])
print('Ci = ',Ci)
ReadMatrix(int(26))
我在print(Ci[0][0])
之后写ReadMatrix(int(26))
时出现错误。
所以我的问题是,如何在程序之外使用Ci
列表。
答案 0 :(得分:0)
将Ci作为参数传递给ReadMatrix。您可以在函数外部的范围内声明Ci,使用Ci作为参数调用ReadMatrix,然后根据需要打印Ci。
def ReadMatrix(numberOfRows,Ci):
for i in range(0,numberOfRows):
Ci.append([]);
f=open("dimer3.out","r")
lines=f.readlines()
numberOfBlocks = math.ceil(numberOfRows / 5)
for i in range(0,int(numberOfBlocks)):
for j in range(i*(numberOfRows+3)+3, i*(numberOfRows+3)+3+numberOfRows):
array = re.split('\s+', lines[j])
for k in range(0,len(array)):
if re.search("\.",array[k]) is not None:
Ci[j - 3 - i*(numberOfRows+3)].append(array[k])
print('Ci = ',Ci)
ReadMatrix(int(26))
Ci=[]
Readmatrix(numRows,Ci)
print Ci