繁体   English   中英

怎么做 '?' 等于一个数字,所以当我浏览一个列表时。 它会将其计为零。 (Python)

[英]How to make '?' equal a number, so when i go through a list. It will count it as a zero. (Python)

正如标题所说,我希望它读取“?” 从 excel 工作表中取出,当它越过它时将其计为零。

for eachLine in train:
    tempList=eachLine.split(',')
    avg0=float(tempList[0])
    ageL.append(avg0)
    avg1=float(tempList[1])
    sL.append(avg1)
    avg2=float(tempList[2])
    rbsL.append(avg2)
    avg3=float(tempList[3])
    fbsL.append(avg3)
    avg4=float(tempList[4])
    ogtL.append(avg4)
    avg5=float(tempList[5])
    hemo.append(avg5)

我知道它丑陋,但它对我有用。 任何帮助将不胜感激,谢谢。

如果我明白你想做什么,你应该创建一个函数来解析每个单元格并返回值的浮点数或检查问号。

def parse_cell(cell):
    try:
        return float(cell)
    except ValueError:
        if cell == "?":
            return 0.0
        raise

然后用这个函数替换每个浮点调用,即

avg0 = parse_cell(tempList[0])
directory = [ageL, sL, rbsL, fbsL, ogtL, hemo]
for eachLine in train:
    tempList=eachLine.split(',')
    for idx, data in tempList:
        try:
            directory[idx].append(float(data))
        except ValueError:
            directory[idx].append(0.)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM