2017-08-15 22 views
0

私は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) 

感謝。

答えて

1

問題は、この行にある:

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

特定の要求が整数と比較することができない結果None戻り、辞書にありません。この値は、エラーが発生した比較が行われたcompare_positionsメソッドに渡されます。

アプリケーションに応じて、ゼロなどのデフォルト値を試す可能性があります

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

ご返信いただきありがとうございます。私は誤って前にすべてのコードを追加していませんでした。ご覧のとおり、 'test_position = Positions [g] [c] .get(sn)'は、iterate_thru関数のキーを反復しているので、辞書に入れておくべきです。 'save_info'関数では、辞書は自動化されていなければなりません。 – NewbieDoo

+0

キーが辞書にある場合、そのキーの値は[なし]にする必要があります。これは 'test_position = Positions [g] [c] .get(sn)または0'で動作するかもしれません。Noneがそのキー位置の値であれば値ゼロが返されます。 – Alexander

関連する問題