【问题标题】:Coercing dictionary values into floats将字典值强制转换为浮点数
【发布时间】:2018-01-23 18:31:26
【问题描述】:

我是 python 新手。我有一个问题,我被卡住了。信息保存在许多字典中。变量 pos 是文件中的浮点数,但 python 将其读取为字符串。我想这就是为什么在调用 compare_positions 函数时,我收到以下错误消息:

Traceback (most recent call last):
File "anotherPythontry.py", line 167, in <module>
  iterate_thru(Genes,c,maxp,p)
File "anotherPythontry.py", line 133, in iterate_thru
  compare_positions(test_position,maxp,sn,g,p,c)
File "anotherPythontry.py", line 103, in compare_positions
  elif (testpos > maxpos and testpos <= maxpos+1):
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

我试过让postestposmaxpos 浮动,但后来我得到了TypeError: float() argument must be a string or a number。我不确定是什么问题。

代码如下:

def hasher():
      return collections.defaultdict(hasher)

Positions = hasher()
LODs = hasher()
whole_line = hasher()
InCommon = hasher()

def save_info(line):
    lod = line[7]
    chr = line[2]
    pos = line[3] #is a float in the file. Is read as a string though
    snp = line[1]
    item = line[10]
    together = '\t'.join(line)
    whole_line[item][snp]=together
    Positions[item][chr][snp]= pos
    LODs[item][chr][snp]=lod
    return snp, item

with open(sys.argv[3],"r") as geneFile:
    gsnp_list = list()
    Genes = []
    for line in geneFile:
            line = line.strip().split("\t")
            type1 = line[0]
            if "SNP_id" or "Minimum" not in line:
                    if type1 == match:
                            snp,item = save_info(line)
                            gsnp_list.append(snp)
                            if item not in Genes:
                                    Genes.append(item)
# A similar block of code is for another file with phenotypes

def compare_positions(testpos,maxsnp,gs,gene,p,c):
    maxpos = Positions[p][c].get(maxsnp)
    if testpos == maxpos:
            InCommon[p][c][maxsnp][gene].append(gs) 
    elif (testpos > maxpos and testpos <= maxpos+1):
            InCommon[p][c][maxsnp][gene].append(gs)
    elif (testpos < maxpos and testpos >= maxpos-1):
            InCommon[p][c][maxsnp][gene].append(gs)

def iterate_thru(Genelist,c,maxp,p):
    for g in Genelist:
            for sn in Positions[g][c].keys():
                    test_position = Positions[g][c].get(sn)
                    compare_positions(test_position,maxp,sn,g,p,c)

for g in Genes: 
    for c in Positions[g].keys():
            chr_SNPlist = ()
            chr_SNPlist = [snp for snp in gsnp_list if snp == Positions[g][c].keys()]
            maxp = get_max(chr_SNPlist,g,c)
            iterate_thru(Phenos,c,maxp,g)

提前致谢。

【问题讨论】:

    标签: python string dictionary numbers typeerror


    【解决方案1】:

    问题出在这一行:

    test_position = Positions[g][c].get(sn)
    

    给定的请求不在字典中,返回None 作为无法与整数比较的结果。然后将该值传递给compare_positions 方法,在该方法中进行比较,从而导致您的错误。

    根据您的应用程序,您可以尝试使用默认值,例如零:

    test_position = Positions[g][c].get(sn, 0)
    

    【讨论】:

    • 感谢您的回复。我之前错误地没有添加所有代码。如您所见,test_position = Positions[g][c].get(sn) 应该在字典中,因为我在 iterate_thru 函数中遍历它的键。在save_info 函数中,字典应该进行自动生存。
    • 如果键在字典中,则该键的值必须为无。这可能会起作用test_position = Positions[g][c].get(sn) or 0,如果 None 是该键位置的值,则将返回值零。
    猜你喜欢
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2011-11-25
    • 2021-11-25
    相关资源
    最近更新 更多