Operations on few dictionaries based on key

python dictionary keys
python dictionary update if key exists
python dictionary with list as value
how to get keys from dictionary in python
dictionary operations
print specific key in dictionary python
print keys and values in dictionary python
python dictionary append

I have 3 dictionaries

demo1={'1':['a','b','c'],'2':['x','y'],...}
demo2={1:['b','c','e'],2:['x','z','w'],...}
coef={'1':5,'2':6,...}

I want output like this

output={1:2/5,2:1/6,...}

where in output value numerator is number of matching values between demo1 and demo2, and denominator is corresponding value in coef.

ex. for key=1 in demo1 and demo2, 'b' and 'c' match, so numerator is 2, and coef for key=1 is 5

I've tried to do it via for loops, but I'm having too much of them, is there more efficient way?

PS: key in demo1 is string, and in demo2 is int, that doesn't matter what returns in output. Either string or int is fine

You can use a dictionary comprehension:

demo1={'1':['a','b','c'],'2':['x','y']}
demo2={1:['b','c','e'],2:['x','z','w']}
coef={'1':5,'2':6}
result = {i:sum(c in demo1[i] and c in demo2[int(i)] for c in set(demo1[i]+demo2[int(i)]))/coef[i] for i in demo1}

Output:

{'1': 0.4, '2': 0.16666666666666666}

Can only immutable objects be used as keys in dictionaries in , What type of objects can be used as keys in dictionaries? Problem Statement – Here are the major tasks that are needed to be performed. Display both the keys and values sorted in alphabetical order by the key. Same as part (ii), but sorted in alphabetical order by the value. First, sort the keys alphabetically using key_value.iterkeys () function. Second, sort the keys alphabetically using sorted

Easier:

print({k:len([i for i in demo1[k] if i in demo2[int(k)]])/v for k,v in coef.items()})

Output:

{'1': 0.4, '2': 0.16666666666666666}

Can a dictionary have two keys of the same value?, Dictionary A dictionary constant consists of a series of key-value pairs enclosed by curly braces { } With dictionaries you The filtered dictionary i.e. newDict now contains filtered elements from the original dictionary i.e. elements whose key is divisible by 2. Similarly we can have conditional filtering based on value field instead of key. But instead of writing code for iteration and condition checking again and again, we move the code to a generic function and

You can convert the values to sets and find the length of the intersections

{k:len(set(demo1.get(k)).intersection(demo2.get(int(k))))/v for k,v in coef.items()}
{'1': 0.4, '2': 0.16666666666666666}

Common Dictionary Operations in Python, Dictionaries have a number of useful built-in methods. The keys method returns what Python 3 calls a view of its underlying keys. A view object has some  Dictionary comprehension is an elegant and concise way to create a new dictionary from an iterable in Python. Dictionary comprehension consists of an expression pair ( key: value ) followed by a for statement inside curly braces {} .

20. Dictionaries, The dictionary stores objects as key-value pairs and can be used to represent complex Use update() to set several key-value pairs at once; Dictionaries and lists, Among other operations, this allows us to slice the list in sequential chunks: Python Dictionary. Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. Key value is provided in the dictionary to make it more optimized.

Use the length of the merged sets

 d = {k: len(set(demo1[k]) & set(demo2[int(k)]))/v for k, v in coef.items()}

Using dictionaries to store data as key-value pairs, Given two dictionaries, you need to find the set of keys that are in both Dictionaries are a good concrete representation for sets in Python, so operations such as the Python 2.2 technique, and 280 microseconds using the filter -​based way. You can save this code into a .py file and run it (a few times, on an otherwise  Varun June 9, 2018 Python : How to Sort a Dictionary by key or Value ? In this article we will discuss how to sort the contents of dictionary by key or value. Suppose we have a dictionary of string and ints i.e. Dictionary is like a hash table that store the elements by calculating hashes of keys and orders of elements in it can not be predicted.

Finding the Intersection of Two Dictionaries, A dictionary is a mutable, unordered set of key-value pairs where each key must be unique. With these concepts in mind, let's review some of the available The most common operation is to add a new element to the end of the list Another built-in method, .sort() , allows us to sort the items in the list  Operations on few dictionaries based on key. Ask Question ex. for key=1 in demo1 and demo2, 'b' and 'c' match, so numerator is 2, and coef for key=1 is 5

Manipulating Lists and Dictionaries in Python, Python dictionary is a container of key-value pairs. is a syntactic construct which creates a dictionary based on existing dictionary. The following examples shows some basic operations with Python dictionaries. basics.py. The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with del . If you store using a key that is already in use, the old value associated with that key is forgotten.

Python dictionaries - working with dictionaries in Python, Defining a Dictionary; Accessing Dictionary Values; Dictionary Keys vs. Restrictions on Dictionary Values; Operators and Built-in Functions; Built-in Dictionary because there are so few limitations on the keys and values that are allowed. Python dictionary is a container of key-value pairs. It is mutable and can contain mixed types. A dictionary is an unordered collection. Python dictionaries are called associative arrays or hash tables in other languages. The keys in a dictionary must be immutable objects like strings or numbers. They must also be unique within a dictionary.

Comments
  • When you mean by:i.e 2/5,1/6 etc... and do you actually want division?, or just a string?
  • @U9-Forward division. I put it in that way for clarity
  • thanks for the info
  • are the keys in demo2 ints or strings?
  • @James, demo1 is string, demo2 is int. That doesn't matter though for output. It can be either string or int
  • but initially demo1 is string and demo2 is int
  • @U9-Forward No problem. It does seem to appear that the keys in demo2 must be integers, however.
  • @Ajax1234, Thank you
  • Quite concise. + 1
  • @Ajax1234 Thanks a lot, i am going to also vote for you :-)