dictionary-comprehension

How to use if/else in a dictionary comprehension?

折月煮酒 提交于 2019-11-27 17:16:30
In python2.7+ exist any way to make something like: { something_if_true if condition else something_if_false for key, value in dict_.items() } I know you can make anything with just 'if' { something_if_true for key, value in dict_.items() if condition} Marcin You've already got it: A if test else B is a valid python expression. The only problem with your dict comprehension as shown is that the place for an expression in a dict comprehension must have two expressions, separated by a colon: { (some_key if condition else default_key):(something_if_true if condition else something_if_false) for

Python dictionary comprehension using locals() gives KeyError

戏子无情 提交于 2019-11-27 09:46:40
>>> a = 1 >>> print { key: locals()[key] for key in ["a"] } Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <dictcomp> KeyError: 'a' How can I create a dictionary with a comprehension like this? A dict comprehension has its own namespace , and locals() in that namespace has no a . Technically speaking, everything but the initial iterable for the outermost iterable (here ["a"] ) is run almost as a nested function with the outermost iterable passed in as an argument. Your code works if you used globals() instead, or created a reference to the

Is it possible to access current object while doing list/dict comprehension in Python?

≡放荡痞女 提交于 2019-11-27 05:37:31
Trying to think of a one-liner to achieve the following ( summing all the values of a key) : >>> data = [('a',1),('b',3),('a',4),('c',9),('b',1),('d',3)] >>> res = {} >>> for tup in data: ... res[tup[0]] = res.setdefault(tup[0],0) + tup[1] ... >>> res {'a': 5, 'c': 9, 'b': 4, 'd': 3} One-liner version without using any imports like itertools,collections etc. { tup[0] : SELF_REFERENCE.setdefault(tup[0],0) + tup[1] for tup in data } Is it possible in Python to use a reference to the object currently being comprehended ? If not, is there any way to achieve this in a one-liner without using any

Nested dictionary comprehension python

我怕爱的太早我们不能终老 提交于 2019-11-26 19:24:59
问题 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 =

How to use if/else in a dictionary comprehension?

六眼飞鱼酱① 提交于 2019-11-26 15:15:56
问题 In python2.7+ exist any way to make something like: { something_if_true if condition else something_if_false for key, value in dict_.items() } I know you can make anything with just 'if' { something_if_true for key, value in dict_.items() if condition} 回答1: You've already got it: A if test else B is a valid python expression. The only problem with your dict comprehension as shown is that the place for an expression in a dict comprehension must have two expressions, separated by a colon: {

Python dictionary comprehension using locals() gives KeyError

你说的曾经没有我的故事 提交于 2019-11-26 14:54:03
问题 >>> a = 1 >>> print { key: locals()[key] for key in ["a"] } Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <dictcomp> KeyError: 'a' How can I create a dictionary with a comprehension like this? 回答1: A dict comprehension has its own namespace , and locals() in that namespace has no a . Technically speaking, everything but the initial iterable for the outermost iterable (here ["a"] ) is run almost as a nested function with the outermost

Is it possible to access current object while doing list/dict comprehension in Python?

情到浓时终转凉″ 提交于 2019-11-26 11:38:56
问题 Trying to think of a one-liner to achieve the following ( summing all the values of a key) : >>> data = [(\'a\',1),(\'b\',3),(\'a\',4),(\'c\',9),(\'b\',1),(\'d\',3)] >>> res = {} >>> for tup in data: ... res[tup[0]] = res.setdefault(tup[0],0) + tup[1] ... >>> res {\'a\': 5, \'c\': 9, \'b\': 4, \'d\': 3} One-liner version without using any imports like itertools,collections etc. { tup[0] : SELF_REFERENCE.setdefault(tup[0],0) + tup[1] for tup in data } Is it possible in Python to use a

Why is there no tuple comprehension in Python?

﹥>﹥吖頭↗ 提交于 2019-11-26 11:32:47
As we all know, there's list comprehension, like [i for i in [1, 2, 3, 4]] and there is dictionary comprehension, like {i:j for i, j in {1: 'a', 2: 'b'}.items()} but (i for i in (1, 2, 3)) will end up in a generator, not a tuple comprehension. Why is that? My guess is that a tuple is immutable, but this does not seem to be the answer. Martijn Pieters You can use a generator expression: tuple(i for i in (1, 2, 3)) but parentheses were already taken for … generator expressions. chepner Raymond Hettinger (one of the Python core developers) had this to say about tuples in a recent tweet : #python

Why is there no tuple comprehension in Python?

江枫思渺然 提交于 2019-11-26 03:29:28
问题 As we all know, there\'s list comprehension, like [i for i in [1, 2, 3, 4]] and there is dictionary comprehension, like {i:j for i, j in {1: \'a\', 2: \'b\'}.items()} but (i for i in (1, 2, 3)) will end up in a generator, not a tuple comprehension. Why is that? My guess is that a tuple is immutable, but this does not seem to be the answer. 回答1: You can use a generator expression: tuple(i for i in (1, 2, 3)) but parentheses were already taken for … generator expressions. 回答2: Raymond Hettinger

Alternative to dict comprehension prior to Python 2.7

风格不统一 提交于 2019-11-26 01:36:25
问题 How can I make the following functionality compatible with versions of Python earlier than Python 2.7? gwfuncs = [reboot, flush_macs, flush_cache, new_gw, revert_gw, send_log] gw_func_dict = {chr(2**i): func for i, func in enumerate(gwfuncs[:8])} 回答1: Use: gw_func_dict = dict((chr(2**i), func) for i, func in enumerate(gwfuncs[:8])) That's the dict() function with a generator expression producing (key, value) pairs. Or, to put it generically, a dict comprehension of the form: {key_expr: value