dicts & lists together

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

    #1

    dicts & lists together

    I have a dict that associates strings to numbers like this {'abc': 1,
    'xyz':2, '123':3,...} I am then given a list of strings ['abc', 'xyz',
    '123',...] and asked to associate a number to each string in the list
    according to the string's value in the dict. I am at a loss as to how to
    do this, can someone show me where to start?
  • Larry Bates

    #2
    Re: dicts & lists together

    Brad Tilley wrote:[color=blue]
    > I have a dict that associates strings to numbers like this {'abc': 1,
    > 'xyz':2, '123':3,...} I am then given a list of strings ['abc', 'xyz',
    > '123',...] and asked to associate a number to each string in the list
    > according to the string's value in the dict. I am at a loss as to how to
    > do this, can someone show me where to start?[/color]
    [color=blue][color=green][color=darkred]
    >>> xdict={'abc': 1, 'xyz':2, '123':3}
    >>> xlist=['abc', 'xyz', '123']
    >>> tlist=[xdict.get(v, None) for v in xlist]
    >>> print tlist[/color][/color][/color]
    [1, 2, 3]

    Larry Bates
    Syscon, Inc.

    Comment

    • Brad Tilley

      #3
      Re: dicts & lists together

      Larry Bates wrote:[color=blue]
      > Brad Tilley wrote:
      >[color=green]
      >> I have a dict that associates strings to numbers like this {'abc': 1,
      >> 'xyz':2, '123':3,...} I am then given a list of strings ['abc', 'xyz',
      >> '123',...] and asked to associate a number to each string in the list
      >> according to the string's value in the dict. I am at a loss as to how
      >> to do this, can someone show me where to start?[/color]
      >
      >[color=green][color=darkred]
      > >>> xdict={'abc': 1, 'xyz':2, '123':3}
      > >>> xlist=['abc', 'xyz', '123']
      > >>> tlist=[xdict.get(v, None) for v in xlist]
      > >>> print tlist[/color][/color]
      > [1, 2, 3]
      >
      > Larry Bates
      > Syscon, Inc.[/color]

      Thank you Larry, I don't understand it, but it works ;)

      Comment

      • Bengt Richter

        #4
        Re: dicts & lists together

        On Tue, 02 Nov 2004 13:45:38 -0500, Brad Tilley <bradtilley@gma il.com> wrote:
        [color=blue]
        >I have a dict that associates strings to numbers like this {'abc': 1,
        >'xyz':2, '123':3,...} I am then given a list of strings ['abc', 'xyz',
        >'123',...] and asked to associate a number to each string in the list
        >according to the string's value in the dict. I am at a loss as to how to
        >do this, can someone show me where to start?[/color]

        Associate as in list of number,string pairs?
        [color=blue][color=green][color=darkred]
        >>> dct = {'abc':1, 'xyz':2, '123':3}
        >>> strings = ['abc','xyz','12 3','error']
        >>> [(dct[s],s) for s in strings][/color][/color][/color]
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        KeyError: 'error'[color=blue][color=green][color=darkred]
        >>> [(dct.get(s, 99999),s) for s in strings][/color][/color][/color]
        [(1, 'abc'), (2, 'xyz'), (3, '123'), (99999, 'error')]

        Regards,
        Bengt Richter

        Comment

        • Pierre Barbier de Reuille

          #5
          Re: dicts &amp; lists together

          Brad Tilley a écrit :[color=blue]
          > Larry Bates wrote:
          >[color=green]
          >> Brad Tilley wrote:
          >>[color=darkred]
          >>> I have a dict that associates strings to numbers like this {'abc': 1,
          >>> 'xyz':2, '123':3,...} I am then given a list of strings ['abc',
          >>> 'xyz', '123',...] and asked to associate a number to each string in
          >>> the list according to the string's value in the dict. I am at a loss
          >>> as to how to do this, can someone show me where to start?[/color]
          >>
          >>
          >>[color=darkred]
          >> >>> xdict={'abc': 1, 'xyz':2, '123':3}
          >> >>> xlist=['abc', 'xyz', '123']
          >> >>> tlist=[xdict.get(v, None) for v in xlist]
          >> >>> print tlist[/color]
          >> [1, 2, 3]
          >>
          >> Larry Bates
          >> Syscon, Inc.[/color]
          >
          >
          > Thank you Larry, I don't understand it, but it works ;)[/color]

          If you don't understand what Larry's solution does, you probably should
          have a look there :



          or look in the web for "list comprehensions" in Python.

          Pierre

          Comment

          • Raymond Hettinger

            #6
            Re: dicts &amp; lists together

            "Brad Tilley" <bradtilley@gma il.com>[color=blue]
            > I have a dict that associates strings to numbers like this {'abc': 1,
            > 'xyz':2, '123':3,...} I am then given a list of strings ['abc', 'xyz',
            > '123',...] and asked to associate a number to each string in the list
            > according to the string's value in the dict. I am at a loss as to how to
            > do this, can someone show me where to start?[/color]

            Is this a homework assignment?


            Raymond Hettinger


            Comment

            Working...