Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43362

Re: use a loop to create lists

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <Thomas.Goebel@ohm-hochschule.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.054
X-Spam-Evidence '*H*': 0.89; '*S*': 0.00; '[0,': 0.09; '[],': 0.09; 'subject:create': 0.09; 'python': 0.11; 'creates': 0.14; 'received:141': 0.14; '2],': 0.16; '[[],': 0.16; 'braces?': 0.16; 'dict': 0.16; 'goebel': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'thu,': 0.19; 'header:User-Agent:1': 0.23; 'fine': 0.24; 'define': 0.26; 'header:In-Reply-To:1': 0.27; 'forgot': 0.30; "i'm": 0.30; 'code': 0.31; "skip:' 10": 0.31; 'keys': 0.31; 'checked': 0.32; 'run': 0.32; 'running': 0.33; 'maybe': 0.34; 'subject:lists': 0.35; 'but': 0.35; 'i.e.': 0.36; "didn't": 0.36; 'thanks': 0.36; 'detail': 0.37; 'too': 0.37; 'list': 0.37; 'list.': 0.37; 'follows:': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'header:Message-Id:1': 0.63; 'finally': 0.65; 'thomas': 0.65; 'between': 0.67; 'header:Reply- To:1': 0.67; 'results': 0.69; 'reply-to:no real name:2**0': 0.71; 'as:': 0.81; 'p.s:': 0.84; '2013,': 0.91; '2013': 0.98
Date Thu, 11 Apr 2013 14:57:41 +0200
From Thomas Goebel <Thomas.Goebel@ohm-hochschule.de>
Organization Technische Hochschule Nuernberg
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130328 Thunderbird/17.0.5
MIME-Version 1.0
To python-list@python.org
Subject Re: use a loop to create lists
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> <CAOjpqbMnZ_17iDj12qFqKU8sTEm7-QdYQAgi5T21R7k6kXFHgg@mail.gmail.com>
In-Reply-To <CAOjpqbMnZ_17iDj12qFqKU8sTEm7-QdYQAgi5T21R7k6kXFHgg@mail.gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To Thomas.Goebel@ohm-hochschule.de
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.468.1365685060.3114.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1365685060 news.xs4all.nl 2581 [2001:888:2000:d::a6]:60957
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43362

Show key headers only | View raw


* On 11/04/2013 14:11, Franz Kelnreiter wrote:
> On Thu, Apr 11, 2013 at 1:46 PM, Thomas Goebel wrote:
>>
>> 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)})
>
> 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:

This code
> [a for a in range(20)] for i in range(20)

won't run because you you didn't define 'i'!

[a for a in range(3)]

will return a list
[0, 1, 2]


To get a dict with n keys we can use a for-loop, too:

d = {}
for n in range(3):
    # Create keys 'list_0' to 'list_n'
    d['list_' + str(n)] = []

So we finally get:
d.keys()
['list_2', 'list_1', 'list_0']

d.values()
[[], [], []]


Now we create a dict with n keys and for every key n we set the value
to a list of m ints:

e = {}
for n in range(3):
    # Create keys 'list_0' to 'list_n'
    e['list_' + str(n)] = []
    for m in range(3):
        # Create a list [0, 1, 2] for every key of e
        e['list_' + str(n)].append(m)

The results is as follows:
e.keys()
['list_2', 'list_1', 'list_0']

e.values()
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

Which is the same as:
f = {'list_' + str(n):[m for m in range(3)] for n in range(3)}

I double checked the code and everything is working fine for me. Maybe
you forgot some braces?

P.S: I'm running on python 2.7.4:
'2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)]'

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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