Justin Carrey
Justin Carrey

Reputation: 3851

searching a value in a list and outputting its key

i have a dictionary, in which each key has a list as its value and those lists are of different sizes. I populated keys and values using add and set(to avoid duplicates). If i output my dictionary, the output is:

blizzard set(['00:13:e8:17:9f:25', '00:21:6a:33:81:50', '58:bc:27:13:37:c9', '00:19:d2:33:ad:9d'])
alpha_jian set(['00:13:e8:17:9f:25'])  

Here, blizzard and alpha_jian are two keys in my dictionary.

Now, i have another text file which has two columns like

00:21:6a:33:81:50    45  
00:13:e8:17:9f:25    59  

As you can see, the first column items are one of the entries in each list of my dictionary. For example, 00:21:6a:33:81:50 belongs to the key 'blizzard' and 00:13:e8:17:9f:25 belongs to the key 'alpha_jian'.

The problem i want is, go through first column items in my text file, and if that column entry is found in dictionary, find its corresponding key, find the length of that corresponding list in the dictionary, and add them in new dictionary, say newDict.
For example 00:21:6a:33:81:50 belongs to blizzard. Hence, newDict entry will be:

newDict[blizzard] = 4  // since the blizzard key corresponds to a list of length 4.  

This is the code i expected to do this task:

newDict = dict()
# myDict is present with entries like specified above
with open("input.txt") as f:
    for line in f:  
        fields = line.split("\t")  
        for key, value in myDict.items():
            if fields[0] == #Some Expression:
                newdict[key] = len(value)  
print newDict  

Here, my question is what should be #Some Expression in my code above. If values are not lists, this is very easy. But how to search in lists? Thanks in advance.

Upvotes: 0

Views: 90

Answers (4)

John La Rooy
John La Rooy

Reputation: 304355

You are looking for in

if fields[0] in value:

But this isn't a very efficient method, as it involves scanning the dict values over and over

You can make a temporary datastructure to help

helper_dict = {k: v for v, x in myDict.items() for k in x}

So your code becomes

helper_dict = {k: v for v, x in myDict.items() for k in x}
with open("input.txt") as f:
    for line in f:  
        fields = line.split("\t")
        key = fields[0]
        if key in helper_dict:
            newdict[helper_dict[key]] = len(myDict[helper_dict[key]])

Upvotes: 1

richsilv
richsilv

Reputation: 8013

if fields[0] in value: should do the trick given that from what you say above every value in the dictionary is a set, whether of length 1 or greater.

It would probably be more efficient to build a new dictionary with keys like '00:13:e8:17:9f:25' (assuming these are unique), and associated values being the number of entries in their set before you start though - that way you will avoid recalculating this stuff repeatedly. Obviously, if the list isn't that long then it doesn't make much difference.

Upvotes: 0

Fredrik
Fredrik

Reputation: 940

Looks like

if fields[0] in value:

should do the trick. I.e. check if the field is a member of the set (this also works for lists, but a bit slower at least if the lists are large).

(note that lists and sets are two different things; one is an ordered container that can contain multiple copies of the same value, the other an unordered container that can contain only one copy of each value.)

You may also want to add a break after the newdict assignment, so you don't keep checking all the other dictionary entries.

Upvotes: 0

hivert
hivert

Reputation: 10667

Doesn't

if fields[0] in value:

solve your problem ? Or I don't understand your question ?

Upvotes: 0

Related Questions