Python question
Erik Max Francis
max at alcyone.com
Sun Jun 25 18:55:18 EDT 2006
Harry wrote:
> 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.449041129349528), ('max',
> 991.6337818245629), ('range', 976.18474069521335), ('mean',
> 496.82174193958127), ('stddev', 304.78275004920454), ('variance',
> 92892.524727555894), ('mode', '46.5818482111'), ('unique_count', '99'),
> ('count', 99.0), ('count_missing', 0.0), ('sum_weight', 99.0)]
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 at 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
More information about the Python-list
mailing list