Python 索引器:列表索引超出范围';
我正在编写一段代码,使用python在ArcMAP中生成一个点形状文件。我有一个50点的1000个随机可能性(在FileA(1000:50)中),我需要尝试所有这些 每个点的坐标X=FileB(:,1)。每个点的坐标Y=FileB(:,2) 为了生成一个序列,我取FileA的第一行,FileA(1,1)中的数字对应于FileB中点1在新序列中的位置 我试图创建一个循环,在这个循环中,我按照FileA的每一行中的位置创建这些序列 我以前有一个帖子: 我将“entry.toInteger()[0]”更改为“int(entry[])。混合语言 我有一个新错误:Python 索引器:列表索引超出范围';,python,loops,indexing,Python,Loops,Indexing,我正在编写一段代码,使用python在ArcMAP中生成一个点形状文件。我有一个50点的1000个随机可能性(在FileA(1000:50)中),我需要尝试所有这些 每个点的坐标X=FileB(:,1)。每个点的坐标Y=FileB(:,2) 为了生成一个序列,我取FileA的第一行,FileA(1,1)中的数字对应于FileB中点1在新序列中的位置 我试图创建一个循环,在这个循环中,我按照FileA的每一行中的位置创建这些序列 我以前有一个帖子: 我将“entry.toInteger()[0]
'tempXYFile.writerow('{0},{1}'.format(coordinates[int(entry)][0],coordinates[int(entry)][1])) IndexError: list index out of range'
我将感谢任何帮助
这是我的代码:
import csv
# create 1000 XY sequences
print 'Start of the script'
sequences = csv.reader(open('50VolcanoPaleoOrder-11-01-2012.csv','rb'),delimiter=',')
coordinates = []
# read coordinates of volcanos and store in memory
print 'Start reading in the coordinates into the memory'
coordinates_file = csv.reader(open('seq50.csv','rb'),delimiter=',')
for coordinate in coordinates[1:]:
coordinates.append(coordinate)
del coordinates_file
i = 1
for sequence in sequences:
print 'sequence # {0}'.format(i)
j = 1
tempXYFile = csv.writer(open('tempXY.csv','w+'),delimiter=',') #add the parameter to create a file if does not exist
for entry in sequence:
tempXYFile.writerow('{0},{1}'.format(coordinates[int(entry)][0],coordinates[int(entry)][1]))
print 'Entry # {0}: {1},{2}'.format(j, coordinates[int(entry)][0],coordinates[int(entry)][1])
j = j + 1
i = i + 1
del tempXYFile
print 'End of Script'
Python中的错误消息不像某些语言中的编译错误那样难以穿透;您应该尝试理解它们告诉您的内容
IndexError: list index out of range
表示您正在使用不存在的索引访问列表。类似于“a=[1,2];print a[79]”的内容会向您发送此消息。在这种情况下,如果问题在行中
tempXYFile.writerow('{0},{1}'.format(coordinates[int(entry)][0],coordinates[int(entry)][1]))
那么很有可能坐标没有int(entry)-th元素,或者坐标[int(entry)]没有第0或第1元素
因此,在该行之前,请尝试插入打印语句:
print int(entry)
print coordinates
print coordinates[int(entry)]
回溯(最后一次调用):文件“C:\Users\Nicolas\Documents\RESEARCH\AVF Voronoi sequence\Voronoi learningprocess.py”,第38行,tempXYFile.writerow(“{0},{1}.”格式(坐标[int(entry)][0],坐标[int(entry)][1]))索引器:列表索引超出范围'不要将其作为注释,将其放入您的问题中(格式正确)。