简体   繁体   中英

Suggestions on the trouble I am having with comparing my two dictionaries?

Here's is my original code:

import csv
with open ("filename1.txt") as f:
    dict1 = {}
    r = csv.reader(f,delimiter="\t")
    for row in r:

        a, b, v = row
        dict1.setdefault((a,b),[]).append(v)

for key in dict1:
    print(key[0])
    print(key[1])
    print(d[key][0]])

This code of course prints which ever column I want. For example here is an example of the text file that it is printing from. I have control over my keys and I can print either column 1, 2, or 3.

7   10165876    0.457295035
6   145989671   0.738336666
3   225038504   0.575564389

However when I implement this code and try to compare two dictionaries, I no longer have control over my keys and I get an error:

import csv
with open ("filename1.txt") as f:
   dict1 = {}
   r = csv.reader(f,delimiter="\t")
   for row in r:

       a, b, v = row
       dict1.setdefault((a,b),[]).append(v)

  #for key in dict1:
       #print(key[0])
       #print(key[1])
       #print(d[key][0]])

with open ("filename2.txt") as f:
   dict2 = {}
   r = csv.reader(f,delimiter="\t")
   for row in r:

      a, b, v = row
      dict2.setdefault((a,b),[]).append(v)

  #for key in dict2:
     #print(key[0])

   count = 0
   for key in dict1:
       for key in dict2:
           if (dict1[0] == dict2[0]) and abs(dict1[1] - dict2[1]) < 10000:
               count +=1

Error:

Traceback (most recent call last):
File "/Users/macbookpro/Desktop/MainDict.py", line 29, in <module>
if (dict1[0] == dict2[0]) and abs(dict1[1] - dict2[1]) < 10000:
KeyError: 0

I've googled and I understand what the error means, but what does it have to do with my code? As I've mentioned before, in the original code I was not getting a KeyError. But now when I have placed everything together I do.

Why am I getting a KeyError and how can I alter my code and fix it?

for key in dict1:
   for key in dict2:
       if (dict1[0] == dict2[0]) and abs(dict1[1] - dict2[1]) < 10000:
           count +=1

You are using key as the iterator variable for both loops. So you essentially overwrite the variable from the outer loop ( dict1 ), making its value inaccessible. Use two different names.

Also, inside the loop body, you are using constant keys to access elements within your dictionaries. This is completely unlike to your original example where you used key to access the dictionary value (using dict1[key] ) and where you printed the key parts ( key[0] and key[1] ). So use those instead:

for key1 in dict1:
   for key2 in dict2:
       if key1[0] == key2[0] and abs(key1[1] - key2[1]) < 10000:
           count += 1

There are several problems. The variable key is allocated twice and never used. The dictionary is probably read as string and not as number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM