another dictionary q

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ruari mactaggart

    another dictionary q

    can this be tidied up ?
    [color=blue][color=green][color=darkred]
    >>>verb={}
    >>>verb[infinitive]=[['','','','','', ''],['','','','','', ''],['','','','',''[/color][/color][/color]
    ,''],['','','','','', ''],['','','','','', ''],['','','','','', '']]

    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


  • Benoit Dejean

    #2
    Re: another dictionary q

    Le Sat, 01 Nov 2003 23:03:24 -0800, ruari mactaggart a écrit :
    [color=blue]
    > can this be tidied up ?
    >[color=green][color=darkred]
    >>>>verb={}
    >>>>verb[infinitive]=[['','','','','', ''],['','','','','', ''],['','','','',''[/color][/color]
    > ,''],['','','','','', ''],['','','','','', ''],['','','','','', '']][/color]

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

    Comment

    • Ben Caradoc-Davies

      #3
      Re: another dictionary q

      On Sun, 02 Nov 2003 01:16:36 +0100, Benoit Dejean <bnet@ifrance.c om> wrote:[color=blue]
      > Le Sat, 01 Nov 2003 23:03:24 -0800, ruari mactaggart a écrit :[color=green][color=darkred]
      >>>>>verb={}
      >>>>>verb[infinitive]=[['','','','','', ''],['','','','','', ''],['','','','',''[/color]
      >> ,''],['','','','','', ''],['','','','','', ''],['','','','','', '']][/color]
      >
      > verb[infinitive]=[['']*6]*6[/color]

      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:
      [color=blue][color=green][color=darkred]
      >>> a=[['',''],['','']]
      >>> b=[['']*2]*2
      >>> a[/color][/color][/color]
      [['', ''], ['', '']][color=blue][color=green][color=darkred]
      >>> b[/color][/color][/color]
      [['', ''], ['', '']]

      They *look* the same, but ...
      [color=blue][color=green][color=darkred]
      >>> a[0][0]=1
      >>> a[/color][/color][/color]
      [[1, ''], ['', '']][color=blue][color=green][color=darkred]
      >>> b[0][0]=1
      >>> b[/color][/color][/color]
      [[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:
      [color=blue][color=green][color=darkred]
      >>> c=[['' for j in range(2)] for i in range(2)]
      >>> c[/color][/color][/color]
      [['', ''], ['', '']][color=blue][color=green][color=darkred]
      >>> c[0][0]=1
      >>> c[/color][/color][/color]
      [[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[i][j] (the dimensions in the ranges are in
      reverse order).

      --
      Ben Caradoc-Davies <ben@wintersun. org>
      A milestone document in the history of human rights, the Universal Declaration of Human Rights set out, for the first time, fundamental human rights to be universally protected. It has been translated into over 500 languages.

      Imprisonment on arrival is the authentic Australian immigration experience.

      Comment

      • Alex Martelli

        #4
        Re: another dictionary q

        ruari mactaggart wrote:
        [color=blue]
        > can this be tidied up ?
        >[color=green][color=darkred]
        >>>>verb={}
        >>>>verb[infinitive]=[['','','','','', ''],['','','','','', ''],['','','','',''[/color][/color]
        > ,''],['','','','','', ''],['','','','','', ''],['','','','','', '']]
        >
        > it is to create a dictionary entry with six empty lists of six items as
        > values.[/color]

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

        is more concise and saves you from counting errors.

        [color=blue]
        > or is it unnecesary ? They represent six tenses with six persons in each
        > for an italian verb translator.[/color]

        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

        Comment

        Working...