简体   繁体   English

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

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

As the title says, I want it to read the "?"正如标题所说,我希望它读取“?” from an excel sheet and count it as a zero when it goes over it.从 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)

I know its ugly, but it works for me.我知道它丑陋,但它对我有用。 Any help would be appreciated, thanks.任何帮助将不胜感激,谢谢。

If I understand what you want to do you should create a function to parse each cell and return either a float of the value or check for a question mark.如果我明白你想做什么,你应该创建一个函数来解析每个单元格并返回值的浮点数或检查问号。

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

Then replace each float call with this function ie然后用这个函数替换每个浮点调用,即

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.

相关问题 如何在清单中计算。 列表混合在Python中 - How to count in List. List is mixed in Python 如何检测列表中的每个数字是否等于或小于零? - How to I detect if each number in the list is equal to or below zero? Discord Bot:这个 Python 代码是为我们的 discord bot 编写的,用于记住我们的图书阅读进度总是返回零。 我怎样才能使这项工作? - Discord Bot: This Python Code written for our discord bot to remember our book reading progress always returns zero. How can I make this work? Python 乌龟:如何使乌龟 go 在通过列表后变为点列表中的一个点 - Python turtle: How can I make the turtle go to a point in a list of points after it goes through the list 如何计算python中一定范围内不为零的行数? - How can I count the number of rows that are not zero in a certain range in python? 我的偶数python函数索引仅返回列表中的第一个偶数。 如何使其返回列表中所有偶数的索引? - My indices of evens python function only returns the first even number in a list. How to make it return the index of all even numbers in a list? 如何获取在python列表中生成的随机数的位置? - How to get the position of a random number generated in a python list.? 我如何将所有这些变量转换成一个列表。 我可以在哪里调用 python 中的随机电影,或者有更好的方法来解决这个问题吗? - How would i convert all these variables into a list. where i could call a random film in python or is there a better way to sort through this? 将零除等于列表中的零 - Make division by zero equal to zero in a list 总结列表中的十进制数。 Python - sum the decimal number in list. python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM