Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43356
| References | <4c47da9e-c632-4855-98a7-0506d8013046@googlegroups.com> <51656F6A.5040103@th-nuernberg.de> <CAOjpqbPmjO1YS2N+931Q6+0NkHALnFocBydBtnhRxjHDf_d-DQ@mail.gmail.com> <5166A297.70104@th-nuernberg.de> <CAOjpqbM80RTXQsxgLJ9R_5X8uezWZQKcZ3F_fN8_vuq1D7_JoA@mail.gmail.com> |
|---|---|
| Date | 2013-04-11 14:11 +0200 |
| Subject | Fwd: use a loop to create lists |
| From | Franz Kelnreiter <kelnreiter@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.464.1365682322.3114.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
---------- Forwarded message ----------
From: Franz Kelnreiter <kelnreiter@gmail.com>
Date: Thu, Apr 11, 2013 at 2:09 PM
Subject: Re: use a loop to create lists
To: Thomas.Goebel@ohm-hochschule.de
On Thu, Apr 11, 2013 at 1:46 PM, Thomas Goebel <
Thomas.Goebel@ohm-hochschule.de> wrote:
> * On 11/04/2013 13:25, Franz Kelnreiter wrote:
> > On Wed, Apr 10, 2013 at 3:55 PM, Thomas Goebel wrote:
> >>
> >> global_list = {'_'.join(['list', str(i)]):[] for i in range(20)}
> >> global_list_1 = global_list['list_0'] --
> >
> > Sorry Thomas, but you had a typo or in any case a wrong syntax
> > concept in your last posting. If you'd like to use list
> > comprehension it should be written as:
> >
> > mydict = {'_'.join(['list', str(i)]):[x for x in range(20)]}
>
> Hi Franz,
>
> the difference between your and my code is that
>
> global_list = {'_'.join(['list', str(i)]):[] for i in range(20)}
>
> creates a dict 'global_list' which has 20 keys named from 'list_0' to
> 'list_19'. The value for all keys is an empty list. If you want to
> create i.e. 20 keys which value is a list with 20 ints you have to use
>
> global_list = ({'_'.join(['list', str(i)]):[a for a in range(20)] for
> i in range(20)})
>
> Your code creates a dict with one key 'list_19' which value is a list
> with 20 ints if you replace str(i) with str(a).
>
> Regards, Tom
>
Tom,
Thanks for your explanation, I think I know what you want to do and I would
very much like to understand your code in detail - maybe I am too stupid -
but when I execute the value part of your code construct:
[a for a in range(20)] for i in range(20)
I get a syntax error, as I exepected (Python 2.6.4 (r264:75708, Oct 26
2009, 08:23:19)).
So how can you get me on the right direction to make your code running on
my machine?
Thank you, Franz
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
use a loop to create lists martaamunar@gmail.com - 2013-04-10 01:40 -0700 Re: use a loop to create lists Chris Angelico <rosuav@gmail.com> - 2013-04-10 18:52 +1000 Re: use a loop to create lists Dave Angel <davea@davea.name> - 2013-04-10 08:36 -0400 Re: use a loop to create lists rusi <rustompmody@gmail.com> - 2013-04-10 08:31 -0700 Re: use a loop to create lists Thomas Goebel <Thomas.Goebel@ohm-hochschule.de> - 2013-04-10 15:55 +0200 Fwd: use a loop to create lists Franz Kelnreiter <kelnreiter@gmail.com> - 2013-04-11 14:11 +0200 Re: use a loop to create lists Thomas Goebel <Thomas.Goebel@ohm-hochschule.de> - 2013-04-11 14:57 +0200 Re: use a loop to create lists Chris Angelico <rosuav@gmail.com> - 2013-04-11 23:22 +1000 Re: Fwd: use a loop to create lists Thomas Goebel <Thomas.Goebel@ohm-hochschule.de> - 2013-04-11 15:43 +0200 Re: use a loop to create lists Franz Kelnreiter <kelnreiter@gmail.com> - 2013-04-11 16:11 +0200 Re: use a loop to create lists Thomas Goebel <Thomas.Goebel@ohm-hochschule.de> - 2013-04-11 16:33 +0200 Re: Fwd: use a loop to create lists Franz Kelnreiter <kelnreiter@gmail.com> - 2013-04-11 16:33 +0200 Re: use a loop to create lists Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-04-11 18:17 -0400
csiph-web