Map mapp中的Map reduce list index超出范围

2024-04-24 05:44:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个地图地图地图,上面有以下代码

#!/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

有什么想法吗?在


Tags: ofnoinfordatatopsysline
1条回答
网友
1楼 · 发布于 2024-04-24 05:44:42

现在有几个问题。您的示例数据似乎是制表符分隔的,但您正在“;”上拆分,请改为“\t”。第六栏不是平衡,是住房,用第五栏。在

如果您要执行许多类似的任务,请看一下python内置的csv模块。在

相关问题 更多 >