You could convert the list
to a tuple
:
>>> count = (1, 0, 0, 2, 0) >>> bins = [ [2.0, 3.0], [3.0, 4.0], [4.0, 5.0], [5.0, 6.0], [6.0, 7.0], [7.0, 8.0] ] >>> { tuple(key): value for (key, value) in zip(bins, count) } { (4.0, 5.0): 0, (3.0, 4.0): 0, (5.0, 6.0): 2, (2.0, 3.0): 1, (6.0, 7.0): 0 }
If you want to serialise to json
, the keys need to be strings. You could convert the bins to strings instead:
>>> {
str(key): value
for (key, value) in zip(bins, count)
} {
'[2.0, 3.0]': 1,
'[4.0, 5.0]': 0,
'[6.0, 7.0]': 0,
'[5.0, 6.0]': 2,
'[3.0, 4.0]': 0
}
>>>
import json
>>>
json.dumps(_)
'{"[2.0, 3.0]": 1, "[4.0, 5.0]": 0, "[6.0, 7.0]": 0, "[5.0, 6.0]": 2, "[3.0, 4.0]": 0}'
Alternatively, just serialise the pairs, and make the dictionary on the receiving end:
>>> zip(bins, count)[([2.0, 3.0], 1), ([3.0, 4.0], 0), ([4.0, 5.0], 0), ([5.0, 6.0], 2), ([6.0, 7.0], 0)]
>>>
import json >>>
json.dumps(_)
'[[[2.0, 3.0], 1], [[3.0, 4.0], 0], [[4.0, 5.0], 0], [[5.0, 6.0], 2], [[6.0, 7.0], 0]]'
{"0": [3.0, 4.0],"0": [4.0, 5.0]}
is not a valid dictionary, as the keys in a dictionary have to be unique. If you really want the entries in count
to be your keys, the best thing I can think of is to make a list
of values for each key:
count = (1, 0, 0, 2, 0, 0, 1, 1, 1, 2) bins = [ [2.0, 3.0], [3.0, 4.0], [4.0, 5.0], [5.0, 6.0], [6.0, 7.0], [7.0, 8.0], [8.0, 9.0], [9.0, 10.0], [10.0, 11.0], [11.0, 12.0], [12.0] ] answer = {} for c, b in zip(count, bins): if c not in answer: answer[c] = [] answer[c].append(b)
In this article, we will discuss how to create and manage a dictionary in which keys can have multiple values.,In this dictionary, keys are strings, and with each key string, we have a list of numbers associated as a value field. So, basically, in this dictionary, multiple values are associated with a key.,Creating a dictionary with multiple values for a key is easy, but the tricky part is appending various values for the keys.Let’s understand by an example,,In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.
Create a dictionary with a list as the value
# Create a dictionary where multiple values are # associated with a key word_freq = { 'is': [1, 3, 4, 8, 10], 'at': [3, 10, 15, 7, 9], 'test': [5, 3, 7, 8, 1], 'this': [2, 3, 5, 6, 11], 'why': [10, 3, 9, 8, 12] }
When we select a key in this dictionary, it gives the value of that key. In this case, it will be a list,
# Create a dictionary where multiple # values are associated with a key word_freq = { 'is': [1, 3, 4, 8, 10], 'at': [3, 10, 15, 7, 9], 'test': [5, 3, 7, 8, 1], 'this': [2, 3, 5, 6, 11], 'why': [10, 3, 9, 8, 12] } # Get multiple values of a key as list value_list = word_freq['at'] print('Values of key "at" are:') print(value_list)
Output:
Values of key "at"
are: [3, 10, 15, 7, 9]
Now let’s use this function to add multiple values for a key,
word_freq = { 'is': [1, 3, 4, 8, 10], 'at': [3, 10, 15, 7, 9], 'test': [5, 3, 7, 8, 1], 'this': [2, 3, 5, 6, 11], 'why': [10, 3, 9, 8, 12] } # Append multiple values for existing key 'at' word_freq = add_values_in_dict(word_freq, 'at', [20, 21, 22]) print('Contents of the dictionary: ') print(word_freq)
Example 2:
# Append multiple values for a new key 'here' word_freq = add_values_in_dict(word_freq, 'here', [10, 11, 12]) print('Contents of the dictionary: ') print(word_freq)
Last Updated : 25 Jan, 2022
Error :
Traceback(most recent call last):
File "46a9aac96614587f5b794e451a8f4f5f.py", line 9, in
print(d['c'])
KeyError: 'c'
Output:
0091 Not Found
Output:
0091 Not Present
By Bernd Klein. Last modified: 29 Jun 2022.
city_population = {
"New York City": 8_550_405,
"Los Angeles": 3_971_883,
"Toronto": 2_731_571,
"Chicago": 2_720_546,
"Houston": 2_296_224,
"Montreal": 1_704_694,
"Calgary": 1_239_220,
"Vancouver": 631_486,
"Boston": 667_137
}
8550405
city_population["New York City"]
city_population["Toronto"]
2731571
city_population["Boston"]