怎么做 '?' 等于一个数字,所以当我通过一个列表。 它会把它算作一个零。 (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) 

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

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

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

然后用这个函数replace每个浮动调用即

 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.)