I'm wondering if there is something similar to list comprehension for
dict (please see the example code below).
d = dict(one=1, two=2)
print d
def fun(d):#Is there a way similar to list comprehension to change the
argument d so that d is changed?
d=dict(three=3)
fun(d)
print d
def fun1(d
On Nov 20, 4:18 am, Peng Yu pengyu...@gmail.com wrote:
I'm wondering if there is something similar to list comprehension for
dict
Yes, but only in Python 3:
{(i, x) for i, x in enumerate('abc')}
{(0, 'a'), (1, 'b'), (2, 'c')}
--
http://mail.python.org/mailman/listinfo/python-list
Peng Yu, 20.11.2009 04:18:
I'm wondering if there is something similar to list comprehension for
dict (please see the example code below).
A list comprehension is an expression that produces a list, e.g.
[ i**2 for i in range(10) ]
Your example below uses a slice assignment.
def fun(d
Stefan Behnel, 20.11.2009 09:24:
You can use d.update(...)
It accepts both another dict as well as a generator expression that
produces item tuples, e.g.
d.update( (i, i**2) for i in range(10) )
This also works, BTW:
d = {}
d.update(value=5)
d
{'value': 5}
Stefan
Peng Yu wrote:
I'm wondering if there is something similar to list comprehension for
dict (please see the example code below).
Do you mean something like this:
{i:i+1 for i in [1,2,3,4]}
{1: 2, 2: 3, 3: 4, 4: 5}
This works in python3, but not in python2
- Patrick
--
http://mail.python.org
Patrick Sabin patrick.just4...@gmail.com writes:
Peng Yu wrote:
I'm wondering if there is something similar to list comprehension for
dict (please see the example code below).
Do you mean something like this:
{i:i+1 for i in [1,2,3,4]}
{1: 2, 2: 3, 3: 4, 4: 5}
This works in python3
Peng Yu wrote:
I'm wondering if there is something similar to list comprehension for
dict (please see the example code below).
Python 3 has list, set, and dict comprehensions.
Don't know about 2.6/7
--
http://mail.python.org/mailman/listinfo/python-list
On Fri, Nov 20, 2009 at 4:18 AM, Peng Yu pengyu...@gmail.com wrote:
I'm wondering if there is something similar to list comprehension for
dict (please see the example code below).
d = dict(one=1, two=2)
print d
def fun(d):#Is there a way similar to list comprehension to change
Michele Simionato wrote:
On Nov 20, 4:18 am, Peng Yu pengyu...@gmail.com wrote:
I'm wondering if there is something similar to list comprehension for
dict
Yes, but only in Python 3:
{(i, x) for i, x in enumerate('abc')}
{(0, 'a'), (1, 'b'), (2, 'c')}
Although the 2.x syntax is hardly
Peng Yu wrote:
I'm wondering if there is something similar to list comprehension for
dict (please see the example code below).
d = dict(one=1, two=2)
print d
def fun(d):#Is there a way similar to list comprehension to change the
argument d so that d is changed?
d=dict(three=3)
fun(d)
print
2009/11/20 Michele Simionato michele.simion...@gmail.com:
Yes, but only in Python 3:
{(i, x) for i, x in enumerate('abc')}
{(0, 'a'), (1, 'b'), (2, 'c')}
In Python 2.x, you can do:
dict((i, x) for i, x in enumerate('abc'))
{0: 'a', 1: 'b', 2: 'c'}
(Works in 2.5 - I can't remember when
NB: I wondered about about dict(one=1, two=2) - why not d = {one:1,
two:2} ? Since you do not write L=list((1, 2)) either. These composed
objects as basic building blocks make Python code so dense and
beautiful, thus using {} means embracing the language's concept.
--
DreiJane schrieb:
NB: I wondered about about dict(one=1, two=2) - why not d = {one:1,
two:2} ? Since you do not write L=list((1, 2)) either. These composed
because it's not working.
{one : 1}
Traceback (most recent call last):
File stdin, line 1, in module
NameError: name 'one' is not
On Fri, 20 Nov 2009 03:08:01 -0800, DreiJane wrote:
NB: I wondered about about dict(one=1, two=2) - why not d = {one:1,
two:2} ?
Because it doesn't work unless you have defined names one and two.
dict(one=1, two=2) uses keyword arguments, namely one and two. This is
the same standard
14 matches
Mail list logo