简体   繁体   English

关于比较我的两个词典时遇到的麻烦的建议?

[英]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. 我可以控制自己的按键,并且可以打印第1、2或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? 我已经在Google上搜索了一下,并且理解了错误的含义,但这与我的代码有什么关系? As I've mentioned before, in the original code I was not getting a KeyError. 如前所述,在原始代码中我没有得到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? 为什么会收到KeyError,如何更改并修复代码?

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. 您正在使用key作为两个循环的迭代器变量。 So you essentially overwrite the variable from the outer loop ( dict1 ), making its value inaccessible. 因此,您实际上从外部循环( dict1 )覆盖了变量,从而使其值不可访问。 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] ). 这与原始示例完全不同,在原始示例中,您使用key来访问字典值(使用dict1[key] ),并在其中打印了关键部分( key[0]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. 该字典可能被读为字符串而不是数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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