list unpack trick?

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

    #1

    list unpack trick?

    I find that I use some list unpacking construct very often:

    name, value = s.split('=',1)

    So that 'a=1' unpack as name='a' and value='1' and 'a=b=c' unpack as
    name='a' and value='b=c'.

    The only issue is when s does not contain the character '=', let's say it
    is 'xyz', the result list has a len of 1 and the unpacking would fail. Is
    there some really handy trick to pack the result list into len of 2 so
    that it unpack as name='xyz' and value=''?

    So more generally, is there an easy way to pad a list into length of n
    with filler items appended at the end?
  • Siegmund Fuehringer

    #2
    Re: list unpack trick?

    On Fri, Jan 21, 2005 at 08:02:38PM -0800, aurora wrote:[color=blue]
    > I find that I use some list unpacking construct very often:
    >
    > name, value = s.split('=',1)
    >
    > So that 'a=1' unpack as name='a' and value='1' and 'a=b=c' unpack as
    > name='a' and value='b=c'.
    >
    > The only issue is when s does not contain the character '=', let's say it
    > is 'xyz', the result list has a len of 1 and the unpacking would fail. Is
    > there some really handy trick to pack the result list into len of 2 so
    > that it unpack as name='xyz' and value=''?[/color]

    what about:
    s.find( '=' )!=-1 and s.split( '=', 1 ) or [s,'']

    bye bye - sifu

    Comment

    • Fredrik Lundh

      #3
      Re: list unpack trick?

      "aurora" wrote:
      [color=blue]
      > The only issue is when s does not contain the character '=', let's say it is 'xyz', the result
      > list has a len of 1 and the unpacking would fail. Is there some really handy trick to pack the
      > result list into len of 2 so that it unpack as name='xyz' and value=''?[/color]

      do you need a trick? spelling it out works just fine:

      try:
      key, value = field.split("=" , 1)
      except:
      key = field; value = ""

      or

      if "=" in field:
      key, value = field.split("=" , 1)
      else:
      key = field; value = ""

      (the former might be slightly more efficient if the "="-less case is uncommon)

      or, if you insist:

      key, value = re.match("([^=]*)(?:=(.*))?", field).groups()

      or

      key, value = (field + "="["=" in field:]).split("=", 1)

      but that's not really worth the typing. better do

      key, value = splitfield(fiel d)

      with a reasonable definition of splitfield (I recommend the try-except form).
      [color=blue]
      > So more generally, is there an easy way to pad a list into length of n with filler items appended
      > at the end?[/color]

      some variants (with varying semantics):

      list = (list + n*[item])[:n]

      or

      list += (n - len(list)) * [item]

      or (readable):

      if len(list) < n:
      list.extend((n - len(list)) * [item])

      etc.

      </F>



      Comment

      • Alex Martelli

        #4
        Re: list unpack trick?

        Fredrik Lundh <fredrik@python ware.com> wrote:
        ...[color=blue]
        > or (readable):
        >
        > if len(list) < n:
        > list.extend((n - len(list)) * [item])[/color]

        I find it just as readable without the redundant if guard -- just:

        alist.extend((n - len(alist)) * [item])

        of course, this guard-less version depends on N*[x] being the empty list
        when N<=0, but AFAIK that's always been the case in Python (and has
        always struck me as a nicely intuitive semantics for that * operator).

        itertools-lovers may prefer:

        alist.extend(it ertools.repeat( item, n-len(alist)))

        a bit less concise but nice in its own way (itertools.repe at gives an
        empty iterator when its 2nd argument is <=0, of course).


        Alex

        Comment

        • Fredrik Lundh

          #5
          Re: list unpack trick?

          Alex Martelli wrote:
          [color=blue][color=green]
          >> or (readable):
          >>
          >> if len(list) < n:
          >> list.extend((n - len(list)) * [item])[/color]
          >
          > I find it just as readable without the redundant if guard -- just:
          >
          > alist.extend((n - len(alist)) * [item])[/color]

          the guard makes it obvious what's going on, also for a reader that doesn't
          know exactly how "*" behaves for negative counts. once you've seen the
          "compare length to limit" and "extend", you don't have to parse the rest of
          the statement.

          </F>



          Comment

          • Alex Martelli

            #6
            Re: list unpack trick?

            Fredrik Lundh <fredrik@python ware.com> wrote:
            [color=blue]
            > Alex Martelli wrote:
            >[color=green][color=darkred]
            > >> or (readable):
            > >>
            > >> if len(list) < n:
            > >> list.extend((n - len(list)) * [item])[/color]
            > >
            > > I find it just as readable without the redundant if guard -- just:
            > >
            > > alist.extend((n - len(alist)) * [item])[/color]
            >
            > the guard makes it obvious what's going on, also for a reader that doesn't
            > know exactly how "*" behaves for negative counts.[/color]

            It does, but it's still redundant, like, say,
            if x < 0:
            x = abs(x)
            makes things obvious even for a reader not knowing exactly how abs
            behaves for positive arguments. Redundancy in the code to try and
            compensate for a reader's lack of Python knowledge is not, IMHO, a
            generally very productive strategy. I understand perfectly well that
            you and others may disagree, but I just thought it worth stating my
            personal opinion in the matter.
            [color=blue]
            > once you've seen the
            > "compare length to limit" and "extend", you don't have to parse the rest of
            > the statement.[/color]

            Sorry, I don't get this -- it seems to me that I _do_ still have to
            "parse the rest of the statement" to understand exactly what alist is
            being extended by.


            Alex

            Comment

            • aurora

              #7
              Re: list unpack trick?

              Thanks. I'm just trying to see if there is some concise syntax available
              without getting into obscurity. As for my purpose Siegmund's suggestion
              works quite well.

              The few forms you have suggested works. But as they refer to list multiple
              times, it need a separate assignment statement like

              list = s.split('=',1)

              I am think more in the line of string.ljust(). So if we have a
              list.ljust(leng th, filler), we can do something like

              name, value = s.split('=',1). ljust(2,'')

              I can always break it down into multiple lines. The good thing about list
              unpacking is its a really compact and obvious syntax.



              On Sat, 22 Jan 2005 08:34:27 +0100, Fredrik Lundh <fredrik@python ware.com>
              wrote:
              ....[color=blue][color=green]
              >> So more generally, is there an easy way to pad a list into length of n
              >> with filler items appended
              >> at the end?[/color]
              >
              > some variants (with varying semantics):
              >
              > list = (list + n*[item])[:n]
              >
              > or
              >
              > list += (n - len(list)) * [item]
              >
              > or (readable):
              >
              > if len(list) < n:
              > list.extend((n - len(list)) * [item])
              >
              > etc.
              >
              > </F>[/color]

              Comment

              Working...