How can I compare keys in two different lists of tuples in Python?

I have two separate lists of tuples as dictionaries. The parentheses are my keys, and the parentheses are my values.

File1:

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

      

File2:

('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 these keys are in the 10,000 range of each other.

If you noticed, I have two keys per value. I would like my code to be formatted this way, while: if the first digit of a separate key in file 1 (eg "8") is equal to the first digit of a separate key in file2 (eg "8") And if the second digit these individual keys (for example, "158116110" and "100949929") are in the range 10,000 apart, count + = 1

This is what I have so 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 doesn't work. I am getting 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 lines.

Why am I getting this error?

+3


source to share


2 answers


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 will return a single file object, but according to your syntax, it tries unpack the file object into x

and y

, two variables causing the problem.

Second, when you do f2-f1

, you are just trying to subtract two file objects (which is not possible).

I think since, according to your example, lines with the same first separate key may be on different lines, it would be better to create two dictionaries for each file first. The dictionary can be in the format -

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

      

Example -



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

      

After creating both dictionaries, you can then iterate over one dictionary and then take the same key from the other dictionary (get the values ​​of that key from both dictionaries) and check if they have values ​​that are within the 10000 range.

Sample 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

      

+1


source


This is the wrong way to go over two things. Try this instead:

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

      



This should work, although I am not aware of the meat of the loop because you did not specify what digit1_f1

and digit1_f2

.

0


source







All Articles