简体   繁体   English

如何在Python中比较两个不同的元组列表中的键?

[英]How to compare keys in two different lists of tuples in Python?

I have two individual lists of tuples in the form of dictionaries. 我有两个单独的字典形式的元组列表。 Located in the parentheses are my keys and in the brackets are my values. 位于括号中的是我的键,括号中是我的值。

File1: 文件1:

('8', '158116110')
['0.00509']
('6', '44338625')
['0.00525']
('3', '127518469')
['2.56E-05']
('9', '141754441')
['0.00585']

File2: 文件2:

('9', '154331672')
['0.165435473']
('8', '100949929')
['0.493410385']
('9', '120747803')
['0.969364472']
('1', '12152579')
['0.669831913']

Working specifically with the keys in both lists, I would like to count how many of those keys are within a 10000 range of one another. 专门研究两个列表中的键,我想计算这些键中有多少键在10000范围内。

If you notice, I have two keys per value. 如果你注意到,我每个值有两个键。 I would like my code to be formatted in such a way whereas: If the first digit of an individual key in File1 (for example '8') equals the first digit of an individual key in File2 (for example '8') AND if the second digits of those individual keys (for example '158116110' and '100949929') are in 10000 range of each other, count+=1 我希望我的代码以这种方式格式化,而:如果File1中的单个键的第一个数字(例如“8”)等于File2中单个键的第一个数字(例如“8”)并且如果这些单独键的第二个数字(例如'158116110'和'100949929')彼此在10000范围内,count + = 1

This is what I have thus far: 这是我到目前为止:

with open('filename1.txt') as f1, open('filename2.txt') as f2:
x, y = f1, f2
count = 0
for x, y in (f1, f2):
    if ((f2 - f1) < 10000) and (digit1_f1 == digit1_f2):
        count +=1
    break

However the code fails. 但是代码失败了。 I get this error: 我收到此错误:

Traceback (most recent call last):
  File "/Users/macbookpro/Desktop/compareDict.py", line 4, in <module>
for x, y in (f1, f2):
  ValueError: too many values to unpack (expected 2)

Both lists are of equal length containing 9524 rows each. 两个列表长度相等,每个包含9524行。

Why I am getting this error? 为什么我收到此错误?

First of all, when you do for x, y in (f1, f2): what really happens is that you are creating a tuple of two file objects, and you are going to iterate over that tuple (not the file objects themselves) , so each iteration would return a single file object, but according to your syntax, it is trying to unpack the file object into x and y , two variables, causing the issue. 首先,当您执行for x, y in (f1, f2):实际发生的是您正在创建两个文件对象的元组,并且您将迭代该元组(而不是文件对象本身),所以每次迭代都会返回一个文件对象,但是根据你的语法,它试图将文件对象解压缩为xy两个变量,导致问题。

Secondly, when you do f2-f1 you are just trying to subtract two file objects (which is not possible). 其次,当你执行f2-f1你只是减去两个文件对象(这是不可能的)。

I think since, according to your example the rows with same first individual key can be in different lines, it would be best to first create two dictionaries for each file. 我认为,根据您的示例,具有相同第一个键的行可以在不同的行中,最好首先为每个文件创建两个词典。 The dictionary can be of format like - 字典的格式可以是 -

d1 = {<first key> : { <second key one>: value , <second key two>: value .... }}

Example - 示例 -

d1 = {'8' : { '158116110' : '0.00509' } , '9' : { '141754441' : '0.00585' } ... }

Once both the dictionaries are created , you can then loop over one dictionary and then take same key from the other dictionary (get values of that key from both dictionary) and check if they have values that are within 10000 range of them. 一旦创建了两个字典,您就可以循环遍历一个字典,然后从另一个字典中获取相同的密钥(从两个字典中获取该密钥的值)并检查它们是否具有在10000范围内的值。

Example code - 示例代码 -

d = {}
for k,v in d1.items():
    v1 = d2.get(k)
    if v1:
        for k1 in v.keys():
            for k2 in v1.keys():
                if abs(int(k1) - int(k2)) < 10000:
                    if k in d:
                        d[k] += 1
                    else:
                        d[k] = 0

That isn't the right way to iterate over two things. 这不是迭代两件事的正确方法。 Instead try: 而是尝试:

for x, y in zip(f1, f2):

This should work, though I don't know about the meat of the loop, because you haven't provided what digit1_f1 and digit1_f2 are. 这应该工作,虽然我不知道循环的肉,因为你没有提供digit1_f1digit1_f2是什么。

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

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