我有一个地图地图地图,上面有以下代码
#!/usr/bin/env python
import sys
myList = []
n = 10 # Number of top N records
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# split data values into list
data = line.split(";")
# convert weight (currently a string) to int
try:
balance = int(data[6])
except ValueError:
# ignore/discard this line
continue
# add (weight, record) touple to list
myList.append( (balance, line) )
# sort list in reverse order
myList.sort(reverse=True)
# keep only first N records
if len(myList) > n:
myList = myList[:n]
# Print top N records
for (k,v) in myList:
print(v)
它在第20行产生以下错误:
^{pr2}$IndexError: list index out of range
进程试图直接访问不存在的管道。在
以下是数据集的示例:
age job marital education default balance housing loan contact day month duration campaign pdays previous poutcome y
30 unemployed married primary no 1787 no no cellular 19 oct 79 1 -1 0 unknown no
33 services married secondary no 4789 yes yes cellular 11 may 220 1 339 4 failure no
有什么想法吗?在
现在有几个问题。您的示例数据似乎是制表符分隔的,但您正在“;”上拆分,请改为“\t”。第六栏不是平衡,是住房,用第五栏。在
如果您要执行许多类似的任务,请看一下python内置的csv模块。在
相关问题 更多 >
编程相关推荐