Hi,
I have two dictionaries and what I would like to do is to search if an item from dict1 is present at least 3 times in dict2. If so, do something.

At the moment, I have something like this

for line2 in list2:
    to_find=line2[3]+line2[4]
    if to_find in dic1:
        new_string=((" ".join(line2[:-1]+[dic1[to_find]])).replace("n/a ","")).split(" ")

But the above does not count the occurrences of "to_find" in dic1. I found also here some other articles suggesting the creation of a counter that increases on each match found, but I find it quite complicated to manage.
Is there any solution that you can share?
Thanks

OK, I guess I found the solution by myself, but please correct me if it is wrong. I would be also curious to know of possible alternatives.

if to_find in dic1 and sum(to_find in dic1 for to_find in dic1)>2:

What are your dictionaries? Python's built-in dictionaries cannot have several occurrences of the same key.

no... it doesn't work

The two dictionaries are different one the other, so I edit them in a way that the search is done only on the specific value they have in common

I don't understand, please post code with complete dictionaries and describe your expected output.

The data I am working with is described in the following post
https://www.daniweb.com/software-development/python/threads/462899/help-with-list-or-dict#post2014289

There, I got to the point where I can get a formatted list of output (given the specified inputs).

Now I would like to count the matching between list1 and list2 for the specific values and get only the result when there are more than 3 matches.
So I would like

"stringX" val1 val2 14.8009136486 41.5433829232 658.00

only if and when 14.8009136486 41.5433829232 658.00 (or any other value from the list1) is present at least 3 times in

"string1" 2275.2 3323.52 14.8016700793 41.5425025232 n/a 
"string1" 3358.08 253.44 14.8012236611 41.5426947232 n/a 
"string2" 3916.8 1365.12 14.8010810554 41.5413741232 n/a 
"string2" 2401.92 2678.4 14.8009136486 41.5433829232 n/a 
"string2" 2177.28 2327.04 14.8009198488 41.5412501232 n/a 
"string2" 2678.4 2787.84 14.8010004521 41.5412191232 n/a 
"string2" 2154.24 2315.52 14.8009198488 41.5412501232 n/a 
"string2" 2229.12 1618.56 14.8009136486 41.5433829232 n/a 
"string3" 4129.92 2499.84 14.8012174609 41.5414981232 n/a 

Assuming that you already have lists of "lines" which are tuples or lists, I suggest something along the line of

from collections import Counter

get_key(line):
    return (line[3], line[4])

# compute the set of all keys in list 1
k1 = set(get_key(line) for line in list1)

count2 = Counter(k for k in (get_key(line) for line in list2) if k in k1)
k2 = set(k for k, v in count2.items() if v >= 3)

result = [line for line in list2 if get_key(line) in k2]

Thanks Gribouillis,
very helpful with minor changes, as you said.

def get_key1(line):
    return (line[0], line[1])

def get_key2(line):
    return (line[3], line[4])

# compute the set of all keys in list 1
k1 = set(get_key1(line) for line in list1)

count2 = Counter(k for k in (get_key2(line) for line in list2) if k in k1)
k2 = set(k for k, v in count2.items() if v >= 3)

result = [line for line in list2 if get_key2(line) in k2]

Can I ask you also if there is a way to merge the two lists?
I need to add to the final "result" the third value of list1 where the two lists match.
Thanks

I need to add to the final "result" the third value of list1 where the two lists match.

You must describe this more precisely. From what I understand you want to alter every line in result with a value extracted from the matching line in list1. It is possible of course, but what will you do if list1 contains more than 1 matching line ?

Sorry but I don't understand.
Let's make an example
list1 has
14.8009136486 41.5433829232 658.00

list2 has
"string1" 2275.2 3323.52 14.8009136486 41.5433829232 n/a

So, since they match, I would like the result to be
"string1" 2275.2 3323.52 14.8009136486 41.5433829232 658.00

List1 is created in a way that all listed values are different, no duplicates.

If list2 has
"string1" 2275.2 3323.52 14.8009136486 41.5433829232 n/a
"string2" 8888.8 8888.52 14.8009136486 41.5433829232 n/a

again my output should be
"string1" 2275.2 3323.52 14.8009136486 41.5433829232 658.00
"string2" 8888.8 8888.52 14.8009136486 41.5433829232 658.00

I hope it is more clear now.
Thanks

Ok, you can change your code like this

def get_item1(line):
    return ((line[0], line[1]), line[2])

def get_key2(line):
    return (line[3], line[4])

k1 = dict(get_item1(line) for line in list1)

count2 = Counter(k for k in (get_key2(line) for line in list2) if k in k1)
k2 = set(k for k, v in count2.items() if v >= 3)

result = []
for line in list2:
    key = get_key2(line)
    if key in k2:
        x = list(line[:-1])
        x.append(k1[key] if line[-1] == 'n/a' else line[-1])
        result.append(x)

Great! Thanks a lot.
Actually there was an extra space at the end of my list2 so I was not getting the expected result.
I fixed it and add a couple of other functions on the final string and now it seems super!
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.