Python question

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

    #1

    Python question

    Hi All,
    It is nice to join the python group. Can someone please help me with
    a python question?
    I have the following object which is like a list of tuples
    What command do I use to get the value corresponding to 'min'?
    This object seems to be non-indexable


    row= [('name', 'x1'), ('min', 15.449041129349 528), ('max',
    991.63378182456 29), ('range', 976.18474069521 335), ('mean',
    496.82174193958 127), ('stddev', 304.78275004920 454), ('variance',
    92892.524727555 894), ('mode', '46.5818482111' ), ('unique_count' , '99'),
    ('count', 99.0), ('count_missing ', 0.0), ('sum_weight', 99.0)]

    Thanks,
    Harry

  • Erik Max Francis

    #2
    Re: Python question

    Harry wrote:
    [color=blue]
    > It is nice to join the python group. Can someone please help me with
    > a python question?
    > I have the following object which is like a list of tuples
    > What command do I use to get the value corresponding to 'min'?
    > This object seems to be non-indexable
    >
    >
    > row= [('name', 'x1'), ('min', 15.449041129349 528), ('max',
    > 991.63378182456 29), ('range', 976.18474069521 335), ('mean',
    > 496.82174193958 127), ('stddev', 304.78275004920 454), ('variance',
    > 92892.524727555 894), ('mode', '46.5818482111' ), ('unique_count' , '99'),
    > ('count', 99.0), ('count_missing ', 0.0), ('sum_weight', 99.0)][/color]

    Iterating would be the best way. There are shorter ways of writing it,
    for instance with list comprehensions, but it's still iteration::

    for key, value in row:
    if key == 'min':
    print value
    break

    or::

    results = [v for k, v in row if k == 'min']
    print results[0]

    Another way, if you plan to access this same data structure in this way
    multiple times before moving on to the next one, would be to turn it
    into a dictionary first::

    d = dict(row)
    print d['min']

    Note that all of these solutions assume that the key you want is indeed
    in there. If it might not be, then you'll have to gracefully handle errors.

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    Divide the fire, and you will the sooner put it out.
    -- Publilius Syrus

    Comment

    • Bruno Desthuilliers

      #3
      Re: Python question

      Harry wrote:[color=blue]
      > Hi All,[/color]
      (snip)[color=blue]
      > I have the following object which is like a list of tuples[/color]
      [color=blue]
      > row= [('name', 'x1'), ('min', 15.449041129349 528), ('max',
      > 991.63378182456 29), ('range', 976.18474069521 335), ('mean',
      > 496.82174193958 127), ('stddev', 304.78275004920 454), ('variance',
      > 92892.524727555 894), ('mode', '46.5818482111' ), ('unique_count' , '99'),
      > ('count', 99.0), ('count_missing ', 0.0), ('sum_weight', 99.0)][/color]

      FWIW, it *is* a list of tuples.
      [color=blue]
      > What command[/color]

      <ot> What's a 'command' ? </ot>
      [color=blue]
      > do I use to get the value corresponding to 'min'?[/color]
      (see below...)
      [color=blue]
      > This object seems to be non-indexable[/color]

      It is - just like any other list. Try :
      print row[0]
      print row[1]
      # etc...

      Now if you want keyword access, try this:
      print dict(row)['min']



      --
      bruno desthuilliers
      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
      p in 'onurb@xiludom. gro'.split('@')])"

      Comment

      Working...