私は3つの異なるデータセットからデータを抽出し、それを組み合わせてキャンペーンの貢献度を調べるプロジェクトを行っています。これを行うために、関連するデータを2つのセットから辞書(canDictとotherDict)にID番号をキーにして必要な情報(パーティー所属)を値として設定しました。それから、私はキーに基づいてパーティー情報を引き出すためのプログラムを書いた(私の3番目のセットにはこれらのID番号も含まれています)、それらを寄付パーティーの雇用主とマッチさせました。それは長らく説明されていませんでしたが、このコードの理解に役立つと思いました。他の辞書からデータを取り出すことによって辞書をコンパイルする
私の問題は、何らかの理由で、私の3番目の辞書(employerDict)がコンパイルされないということです。このステップが終わるまでに、私は雇用者をキーとするタプルと値としてのタプルのリストを持っているべきですが、それを実行した後は辞書は空白のままです。私はこの行を十数回回り、私の髪を引っ張っている - 私の人生はなぜそれがうまくいかないと思うことができないので、答えを探すのが難しくなっている。私は文脈から理解しやすくするために、ほとんどすべての行にコメントしました。 誰かが私の間違いを見つけられますか?
アップデート:私はプログラムがすべてで反復処理されたかどうかを確認するためにループの最も外側に、カウンターを追加し、N。
アップデート2:はI [0] canDictまたはotherDictに存在しなかった場合には、可変party
の作成のデータにIDを別のif文を追加しました。コメントから既に提案されている修正を追加しました。
n=0
with open(path3) as f: # path3 is a txt file
for line in f:
n+=1
if n % 10000 == 0:
print(n)
data = line.split("|") # Splitting each line into its entries (delimited by the symbol |)
party = canDict.get(data[0]) # data[0] is an ID number. canDict and otherDict contain these IDs as keys with party affiliations as values
if party is None:
party = otherDict[data[0]] # If there is no matching ID number in canDict, search otherDict
if party is None:
party = 'Other'
else:
print('ERROR: party is None')
x = (party, int(data[14])) # Creating a tuple of the the party (found through the loop) and an integer amount from the file path3
employer = data[11] # Index 11 in path3 is the employer of the person
if employer != '':
value = employerDict.get(employer) # If the employer field is not blank, see if this employer is already a key in employerDict
if value is None:
employerDict[employer] = [x] # If the key does not exist, create it and add a list including the tuple x as its value
else:
employerDict[employer].append(x) # If it does exist, add the tuple x to the existing value
else:
print('ERROR: employer == ''')
私の直感は 'employer!= '''は常にfalseと評価されます。 2番目のif文の前に 'print(employer!= '')'を試してください。 –
あなたの5行目で、 'data [0]'は 'None'(' split'に由来)である理由はありません。ですから、5行目はおそらく 'もしパーティーがNoneならば'でしょう。 –
@AndrewJenkins考えてくれてありがとうございます:/私はカウンタを追加していました。ちょうど反復していて(更新を参照)、ゼロの出力も返しています。 – boop