dictionary-comprehension

Why is this loop faster than a dictionary comprehension for creating a dictionary?

↘锁芯ラ 提交于 2019-12-03 09:21:06
I don't come from a software/computer science background but I love to code in Python and can generally understand why things are faster. I am really curious to know why this for loop runs faster than the dictionary comprehension. Any insights? Problem : Given a dictionary a with these keys and values, return a dictionary with the values as keys and the keys as values. (challenge: do this in one line) and the code a = {'a':'hi','b':'hey','c':'yo'} b = {} for i,j in a.items(): b[j]=i %% timeit 932 ns ± 37.2 ns per loop b = {v: k for k, v in a.items()} %% timeit 1.08 µs ± 16.4 ns per loop You

Python: Convert table to string to key:value pairs and store in dict

谁都会走 提交于 2019-12-02 03:55:45
I getting data from subprocess command as a string. I want to store this data in a dict. How best do I achieve this? Here is data example: (I have returned this as a string from subprocess.) NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 140.7M 1 loop /snap/gnome-3-26-1604/82 loop1 7:1 0 89.3M 1 loop /snap/core/6673 sda 8:0 0 11G 0 disk ├─sda1 8:1 0 10.9G 0 part / ├─sda14 8:14 0 4M 0 part └─sda15 8:15 0 106M 0 part /boot/efi Here is the output I want: {block device 1: { "name" : value, "maj:min" : value, "RM" : value, "SIZE" : value, "RO": value, "TYPE": value, "MOUNTPOINT" : value},

Is a python dict comprehension always “last wins” if there are duplicate keys

筅森魡賤 提交于 2019-12-01 15:32:45
问题 If I create a python dictionary with a dict comprehension, but there are duplicate keys, am I guaranteed that the last item will the the one that ends up in the final dictionary? It's not clear to me from looking at https://www.python.org/dev/peps/pep-0274/? new_dict = {k:v for k,v in [(1,100),(2,200),(3,300),(1,111)]} new_dict[1] #is this guaranteed to be 111, rather than 100? 回答1: Last item wins. The best documentation I can find for this is in the Python 3 language reference, section 6.2.7

Conditional expressions in Python Dictionary comprehensions

半世苍凉 提交于 2019-11-30 07:18:34
a = {"hello" : "world", "cat":"bat"} # Trying to achieve this # Form a new dictionary only with keys with "hello" and their values b = {"hello" : "world"} # This didn't work b = dict( (key, value) if key == "hello" for (key, value) in a.items()) Any suggestions on how to include a conditional expression in dictionary comprehension to decide if key, value tuple should be included in the new dictionary Move the if at the end: b = dict( (key, value) for (key, value) in a.items() if key == "hello" ) You can even use dict-comprehension ( dict(...) is not one, you are just using the dict factory

Python--Finding Parent Keys for a specific value in a nested dictionary

我的未来我决定 提交于 2019-11-30 07:04:41
I am struggling to process a nested dictionary, and return the nested Parent Keys, for a specific Value, when the Value may exist more than once in the nested dictionary. For example: example_dict = { 'key1' : 'value1', 'key2' : 'value2', 'key3' : { 'key3a': 'value3a' }, 'key4' : { 'key4a': { 'key4aa': 'value4aa', 'key4ab': 'value4ab', 'key4ac': 'value1'}, 'key4b': 'value4b'} } You will notice that 'value1' appears twice in the above dictionary, and I would like to create a function that returns either a single list, or a series of lists, that identify the different Parent Keys, which in this

Conditional expressions in Python Dictionary comprehensions

拥有回忆 提交于 2019-11-29 09:25:17
问题 a = {"hello" : "world", "cat":"bat"} # Trying to achieve this # Form a new dictionary only with keys with "hello" and their values b = {"hello" : "world"} # This didn't work b = dict( (key, value) if key == "hello" for (key, value) in a.items()) Any suggestions on how to include a conditional expression in dictionary comprehension to decide if key, value tuple should be included in the new dictionary 回答1: Move the if at the end: b = dict( (key, value) for (key, value) in a.items() if key ==

Python--Finding Parent Keys for a specific value in a nested dictionary

孤者浪人 提交于 2019-11-29 07:33:30
问题 I am struggling to process a nested dictionary, and return the nested Parent Keys, for a specific Value, when the Value may exist more than once in the nested dictionary. For example: example_dict = { 'key1' : 'value1', 'key2' : 'value2', 'key3' : { 'key3a': 'value3a' }, 'key4' : { 'key4a': { 'key4aa': 'value4aa', 'key4ab': 'value4ab', 'key4ac': 'value1'}, 'key4b': 'value4b'} } You will notice that 'value1' appears twice in the above dictionary, and I would like to create a function that

Is a python dict comprehension always “last wins” if there are duplicate keys

送分小仙女□ 提交于 2019-11-28 13:26:55
If I create a python dictionary with a dict comprehension, but there are duplicate keys, am I guaranteed that the last item will the the one that ends up in the final dictionary? It's not clear to me from looking at https://www.python.org/dev/peps/pep-0274/ ? new_dict = {k:v for k,v in [(1,100),(2,200),(3,300),(1,111)]} new_dict[1] #is this guaranteed to be 111, rather than 100? Last item wins. The best documentation I can find for this is in the Python 3 language reference, section 6.2.7 : A dict comprehension, in contrast to list and set comprehensions, needs two expressions separated with a

OrderedDict comprehensions

淺唱寂寞╮ 提交于 2019-11-28 07:54:57
Can I extend syntax in python for dict comprehensions for other dicts, like the OrderedDict in collections module or my own types which inherit from dict ? Just rebinding the dict name obviously doesn't work, the {key: value} comprehension syntax still gives you a plain old dict for comprehensions and literals. >>> from collections import OrderedDict >>> olddict, dict = dict, OrderedDict >>> {i: i*i for i in range(3)}.__class__ <type 'dict'> So, if it's possible how would I go about doing that? It's OK if it only works in CPython. For syntax I guess I would try it with a O{k: v} prefix like we

Nested dictionary comprehension python

丶灬走出姿态 提交于 2019-11-27 18:48:45
I'm having trouble understanding nested dictionary comprehensions in Python 3. The result I'm getting from the example below outputs the correct structure without error, but only includes one of the inner key: value pairs. I haven't found an example of a nested dictionary comprehension like this; Googling "nested dictionary comprehension python" shows legacy examples, non-nested comprehensions, or answers solved using a different approach. I may be using the wrong syntax. Example: data = {outer_k: {inner_k: myfunc(inner_v)} for outer_k, outer_v in outer_dict.items() for inner_k, inner_v in