another dictionary q

Discussion in 'Python' started by Benoit Dejean, Nov 2, 2003.

  1. Le Sat, 01 Nov 2003 23:03:24 -0800, ruari mactaggart a écrit :

    > can this be tidied up ?
    >
    >>>>verb={}
    >>>>verb[infinitive]=[['','','','','',''],['','','','','',''],['','','','',''

    > ,''],['','','','','',''],['','','','','',''],['','','','','','']]


    verb[infinitive]=[['']*6]*6
     
    Benoit Dejean, Nov 2, 2003
    #1
    1. Advertising

  2. On Sun, 02 Nov 2003 01:16:36 +0100, Benoit Dejean <> wrote:
    > Le Sat, 01 Nov 2003 23:03:24 -0800, ruari mactaggart a écrit :
    >>>>>verb={}
    >>>>>verb[infinitive]=[['','','','','',''],['','','','','',''],['','','','',''

    >> ,''],['','','','','',''],['','','','','',''],['','','','','','']]

    >
    > verb[infinitive]=[['']*6]*6


    These are *not* the same thing. The use of "*" on mutable sequences is a nasty
    trap for the unwary, and highlights the difference between copy and reference
    semantics in Python. The "*" operator does not copy.

    In the first example, the inner lists are distinct, whereas in the second
    example, the same inner list is present multiple times in the outer list. I'll
    use length two for brevity:

    >>> a=[['',''],['','']]
    >>> b=[['']*2]*2
    >>> a

    [['', ''], ['', '']]
    >>> b

    [['', ''], ['', '']]

    They *look* the same, but ...

    >>> a[0][0]=1
    >>> a

    [[1, ''], ['', '']]
    >>> b[0][0]=1
    >>> b

    [[1, ''], [1, '']]

    While b *looks* the same as a, it contains one list multiple times, so if you
    update one list, it affects all the others.

    List comprehensions of list comprehensions are a convenient and succinct way of
    creating lists of distinct lists:

    >>> c=[['' for j in range(2)] for i in range(2)]
    >>> c

    [['', ''], ['', '']]
    >>> c[0][0]=1
    >>> c

    [[1, ''], ['', '']]

    The indices (i and j) in the list comprehensions are not used in this case:
    they are chosen to indicate the meaning of the expression. The innermost
    elements of c are accessed by c[j] (the dimensions in the ranges are in
    reverse order).

    --
    Ben Caradoc-Davies <>
    http://wintersun.org/
    Imprisonment on arrival is the authentic Australian immigration experience.
     
    Ben Caradoc-Davies, Nov 2, 2003
    #2
    1. Advertising

  3. can this be tidied up ?

    >>>verb={}
    >>>verb[infinitive]=[['','','','','',''],['','','','','',''],['','','','',''

    ,''],['','','','','',''],['','','','','',''],['','','','','','']]

    it is to create a dictionary entry with six empty lists of six items as
    values.

    or is it unnecesary ? They represent six tenses with six persons in each for
    an italian verb translator.

    ruari
     
    ruari mactaggart, Nov 2, 2003
    #3
  4. ruari mactaggart wrote:

    > can this be tidied up ?
    >
    >>>>verb={}
    >>>>verb[infinitive]=[['','','','','',''],['','','','','',''],['','','','',''

    > ,''],['','','','','',''],['','','','','',''],['','','','','','']]
    >
    > it is to create a dictionary entry with six empty lists of six items as
    > values.


    verb[infinitive] = [ [""]*6 for i in range(6) ]

    is more concise and saves you from counting errors.


    > or is it unnecesary ? They represent six tenses with six persons in each
    > for an italian verb translator.


    Whether it's necessary or not depends on how your application uses those
    data, so I'm not sure how I can answer the question (despite being
    Italian:).


    Alex
     
    Alex Martelli, Nov 2, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    464
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    349
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,267
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    561
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    206
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page