Why does one work, but not the other?

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

    Why does one work, but not the other?

    I've done this before:

    data = [self.cong.tm[k] for k in self.cong.tm.li]
    #li is list, tm is dict

    instead of:

    for k in self.cong.tm.li :
    data.append(sel f.cong.tm[k])

    but when I try:

    self.liststore = [[item] for item in data]


    instead of:

    for item in data:
    self.liststore. append([item])

    I get an empty list! What gives??

    jonathon
  • Erik Max Francis

    #2
    Re: Why does one work, but not the other?

    j_mckitrick wrote:
    [color=blue]
    > but when I try:
    >
    > self.liststore = [[item] for item in data]
    >
    > instead of:
    >
    > for item in data:
    > self.liststore. append([item])
    >
    > I get an empty list! What gives??[/color]

    You're probably doing something else wrong; this fragment works fine:
    [color=blue][color=green][color=darkred]
    >>> data = [1, 2, 3, 4]
    >>> [[item] for item in data][/color][/color][/color]
    [[1], [2], [3], [4]]

    --
    __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ Ferocious, aren't I?
    -- Lt. Vincent Hanna

    Comment

    • Adonis

      #3
      Re: Why does one work, but not the other?

      ----- Original Message -----
      From: "j_mckitric k" <j_mckitrick@bi gfoot.com>
      Newsgroups: comp.lang.pytho n
      Sent: Thursday, June 17, 2004 9:54 PM
      Subject: Why does one work, but not the other?

      [color=blue]
      > I've done this before:
      >
      > data = [self.cong.tm[k] for k in self.cong.tm.li]
      > #li is list, tm is dict
      >
      > instead of:
      >
      > for k in self.cong.tm.li :
      > data.append(sel f.cong.tm[k])
      >
      > but when I try:
      >
      > self.liststore = [[item] for item in data]
      >
      >
      > instead of:
      >
      > for item in data:
      > self.liststore. append([item])
      >
      > I get an empty list! What gives??
      >
      > jonathon[/color]
      [color=blue][color=green][color=darkred]
      >>> tm = {'a':1, 'b':2, 'c':3}
      >>> li = tm.keys()
      >>> data = [tm[k] for k in li]
      >>> data[/color][/color][/color]
      [1, 3, 2][color=blue][color=green][color=darkred]
      >>> # also you can do
      >>> data2 = [tm[k] for k in tm]
      >>> data2[/color][/color][/color]
      [1, 3, 2]

      Check the dictionary, make sure it has entries?
      When supplying the dictionary with elements given by the list, make sure the
      keys exist.

      or you can do:[color=blue][color=green][color=darkred]
      >>> data = [tm.get(k) for k in li][/color][/color][/color]

      Will return None if a key does not exist to further debug.

      Hope this helps.

      Adonis


      Comment

      • j_mckitrick

        #4
        Re: Why does one work, but not the other?

        Thanks for the help, guys. I found the problem. The liststore cannot
        be redefined once set up for the TreeView. I can only clear/append to
        it.

        But I'm still on my mission to replace 'for' with list comprehensions
        where possible, according to the article on optimization on the python
        site.


        That being said, is there a way to write this as a comprehension? I
        can't figure out how to do so and get k into the key correctly. I'm
        just trying to save a dictionary via anydbm.

        for k, v in self.options.it ems():
        db[k] = str(v)

        jonathon

        Comment

        • Peter Otten

          #5
          Re: Why does one work, but not the other?

          j_mckitrick wrote:
          [color=blue]
          > But I'm still on my mission to replace 'for' with list comprehensions
          > where possible, according to the article on optimization on the python
          > site.[/color]

          I don't know the article, but I assume it doesn't tell list comprehensions
          are always faster/better.
          [color=blue]
          > That being said, is there a way to write this as a comprehension? I
          > can't figure out how to do so and get k into the key correctly. I'm
          > just trying to save a dictionary via anydbm.
          >
          > for k, v in self.options.it ems():
          > db[k] = str(v)[/color]

          Yes,
          [color=blue][color=green][color=darkred]
          >>> dk = {1:2, 3:4}
          >>> options = {1:4, 2:6, 3:8}
          >>> dk.update(dict([(k, str(v)) for (k, v) in options.iterite ms()]))
          >>> dk[/color][/color][/color]
          {1: '4', 2: '6', 3: '8'}[color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

          but why would you trade a muddy comprehension for a clean loop? The for loop
          is clearer (and faster, I suppose) here. Remember that list comprehensions
          are a means rather than an end.

          With 2.4 that may be a different story, as the above will reduce (I think)
          to

          dk.update((k, str(v)) for (k, v) in options.iterite ms())

          However, some overhead (generating throwaway tuples) is likely to remain.

          Peter

          Comment

          • j_mckitrick

            #6
            Re: Why does one work, but not the other?

            Peter Otten <__peter__@web. de> wrote in message news:<cauk4d$b4 n$00$1@news.t-online.com>...[color=blue]
            > j_mckitrick wrote:
            >[color=green]
            > > But I'm still on my mission to replace 'for' with list comprehensions
            > > where possible, according to the article on optimization on the python
            > > site.[/color]
            >
            > I don't know the article, but I assume it doesn't tell list comprehensions
            > are always faster/better.[/color]

            from http://www.python.org/doc/essays/list2str.html:

            Try to use map(), filter() or reduce() to replace an explicit for
            loop, but only if you can use a built-in function: map with a built-in
            function beats for loop, but a for loop with in-line code beats map
            with a lambda function!


            I remember another, but can't find it right now.

            Comment

            • Terry Reedy

              #7
              Re: Why does one work, but not the other?


              "j_mckitric k" <j_mckitrick@bi gfoot.com> wrote in message
              news:ec6dce8b.0 406171754.5d5d8 4db@posting.goo gle.com...[color=blue]
              > I've done this before:
              >
              > data = [self.cong.tm[k] for k in self.cong.tm.li]
              > #li is list, tm is dict
              >
              > instead of:
              >
              > for k in self.cong.tm.li :
              > data.append(sel f.cong.tm[k])
              >
              > but when I try:
              >
              > self.liststore = [[item] for item in data]
              >
              >
              > instead of:
              >
              > for item in data:
              > self.liststore. append([item])
              >
              > I get an empty list! What gives??[/color]

              For questions like this, about supposedly anomalous behavior, you usually
              need to give actual input and output, reduced to the minimum code needed to
              show the purported behavior. Otherwise, the easiest guess is that you did
              not use the same value of data in the two snippets;-)

              Terry J. Reedy




              Comment

              Working...