0

I have a python dict as below:

a={
  u'data': [
    [u'2013-03-19', u'1363762799', u'4004658'],
    [u'2013-03-20', u'1363849199', u'4756668']
  ],
  u'columns': [
    u'dt_pst',
    u'timestamp',
    u'count'
  ]
}

How can I get the max value for key u'timestamp'? I want this number: 1363849199.

Thanks!

1 Answer 1

1

Convert it into a usable format first:

>>> d = {row[0]: row[1:] for row in zip(a['columns'], *a['data'])}
>>> d
    {u'count': (u'4004658', u'4756668'),
 u'dt_pst': (u'2013-03-19', u'2013-03-20'),
 u'timestamp': (u'1363762799', u'1363849199')}

If you're using 2.6, dict comprehensions won't work. You need to use the dict() constructor:

d = dict((row[0], row[1:]) for row in zip(a['columns'], *a['data']))

Now, you can just use max:

>>> max(d['timestamp'], key=int)
    u'1363849199'
>>> max(map(int, d['timestamp']))
    1363849199
Sign up to request clarification or add additional context in comments.

2 Comments

I got this error: File "<stdin>", line 1 df = {row[0]: row[1:] for row in zip(a['columns'], *a['data'])} ^ SyntaxError: invalid syntax when trying to run: d = {row[0]: row[1:] for row in zip(a['columns'], *a['data'])}
@tonystarkix: That syntax won't work for 2.6. See my answer for the 2.6-compatible syntax.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.