467,923 Members | 1,250 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Sorting dictionary by 'sub' value

I have a dictionary of images. I wish to sort the dictionary 'v' by a
dictionary value using python 2.3. The dictionary value is the date
attribute as shown here:

v[imagename][9]['date']

This attribute is an extracted EXIF value from the following set:

data element [9] of v[imagename]:

{'now' : datetime.date(2005, 3, 7),
'y' : (0x011B) Ratio=72 @ 182,
'ctime' : datetime.date(2005, 3, 7),
'width' : (0xA002) Long=1024 @ 434,
'length' : (0xA003) Long=768 @ 446,
'date' : (0x9004) ASCII=2004:12:07 00:18:20 @ 514,
'x' : (0x011A) Ratio=72 @ 174,
'model' : (0x0110) ASCII=PENTAX Optio 330 @ 156,
'size' : 367415L,
'orientation' : (0x0112) Short=1 @ 42}

Thanks,
Rory

--
Rory Campbell-Lange
<ro**@campbell-lange.net>
<www.campbell-lange.net>
Jul 18 '05 #1
  • viewed: 2646
Share:
4 Replies
> I have a dictionary of images. I wish to sort the dictionary 'v' by a
dictionary value using python 2.3. The dictionary value is the date
attribute as shown here:

v[imagename][9]['date']

This attribute is an extracted EXIF value from the following set:

data element [9] of v[imagename]:

{'now' : datetime.date(2005, 3, 7),
'y' : (0x011B) Ratio=72 @ 182,
'ctime' : datetime.date(2005, 3, 7),
'width' : (0xA002) Long=1024 @ 434,
'length' : (0xA003) Long=768 @ 446,
'date' : (0x9004) ASCII=2004:12:07 00:18:20 @ 514,
'x' : (0x011A) Ratio=72 @ 174,
'model' : (0x0110) ASCII=PENTAX Optio 330 @ 156,
'size' : 367415L,
'orientation' : (0x0112) Short=1 @ 42}

You can't sort dicts - they don't impose an order on either key or value.
There are ordered dict implementations out there, but AFAIK the only keep
the keys sorted, or maybe the (key,values) in the insertion order.

But maybe this helps you:

l = v.items()
l.sort(lambda a, b: cmp(a[9]['date'], b[9]['date'])
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Diez B. Roggisch wrote:
I have a dictionary of images. I wish to sort the dictionary 'v' by a
dictionary value using python 2.3. The dictionary value is the date
attribute as shown here:

v[imagename][9]['date']
...


You can't sort dicts - they don't impose an order on either key or value.
There are ordered dict implementations out there, but AFAIK the only keep
the keys sorted, or maybe the (key,values) in the insertion order.

But maybe this helps you:

l = v.items()
l.sort(lambda a, b: cmp(a[9]['date'], b[9]['date'])


In 2.4, this is simple:

ordered_keys = sorted(v, key=lambda name: v[name][9]['date'])

In 2.3, or earlier, use "decorate-sort-undecorate":

decorated = [(value[9]['date'], key)
for key, value in v.iteritems()]
decorated.sort()
result = [key for key, date in decorated]

--Scott David Daniels
Sc***********@Acm.Org

Jul 18 '05 #3
Thank you all very much for your help.

I did the following and it works:

imgs=v.keys()
imgs.sort(lambda a,b: cmp(
time.strptime(str(v[a][9]['date']), '%Y:%m:%d %H:%M:%S'),
time.strptime(str(v[b][9]['date']), '%Y:%m:%d %H:%M:%S'))
)
for i in imgs:
...

Regards,
Rory

On 08/03/05, Diez B. Roggisch (de*********@web.de) wrote:
l = v.items()
l.sort(lambda a, b: cmp(a[9]['date'], b[9]['date'])


On 08/03/05, Scott David Daniels (Sc***********@Acm.Org) wrote:
You can't sort dicts - they don't impose an order on either key or value.
There are ordered dict implementations out there, but AFAIK the only keep
the keys sorted, or maybe the (key,values) in the insertion order.

But maybe this helps you:

l = v.items()
l.sort(lambda a, b: cmp(a[9]['date'], b[9]['date'])


In 2.4, this is simple:

ordered_keys = sorted(v, key=lambda name: v[name][9]['date'])

In 2.3, or earlier, use "decorate-sort-undecorate":

decorated = [(value[9]['date'], key)
for key, value in v.iteritems()]
decorated.sort()
result = [key for key, date in decorated]


On 08/03/05, Batista, Facundo (FB******@uniFON.com.ar) wrote:
temp_list = [ (x[1][1], x[0]) for x in d.items() ] .... temp_list.sort()
for (tmp, key) in temp_list:

--
Rory Campbell-Lange
<ro**@campbell-lange.net>
<www.campbell-lange.net>
Jul 18 '05 #4
Rory Campbell-Lange wrote:
Thank you all very much for your help.

I did the following and it works:

imgs=v.keys()
imgs.sort(lambda a,b: cmp(
time.strptime(str(v[a][9]['date']), '%Y:%m:%d %H:%M:%S'),
time.strptime(str(v[b][9]['date']), '%Y:%m:%d %H:%M:%S'))
)
for i in imgs:
...


Cool. If you ever find that this is a speed problem, it's worth
pointing out that the decorate-sort-undecorate pattern is usually
slightly faster:

------------------------------ test.py ------------------------------
import datetime
import time

# returns data in a similar format to yours
def get_data(n):
today = datetime.datetime(2005, 3, 8)
deltas = [datetime.timedelta(seconds=1e6*i)
for i in xrange(-n, n)]
times = [(today + delta).strftime('%Y:%m:%d %H:%M:%S')
for delta in deltas]
return dict([(i, {9:{'date':time}})
for i, time in enumerate(times)])

def sortcmp(data):
imgs = data.keys()
imgs.sort(lambda a,b: cmp(
time.strptime(str(data[a][9]['date']),
'%Y:%m:%d %H:%M:%S'),
time.strptime(str(data[b][9]['date']),
'%Y:%m:%d %H:%M:%S'))
)
return imgs

def sortdsu(data):
decorated = [(time.strptime(str(data[key][9]['date']),
'%Y:%m:%d %H:%M:%S'), key)
for key in data]
decorated.sort()
return [key for date, key in decorated]

# Requires 2.4
def sortkey(data):
def value(key):
return time.strptime(str(data[key][9]['date']),
'%Y:%m:%d %H:%M:%S')
return sorted(data, key=value)
---------------------------------------------------------------------

And the timing results:

[D:\Steve]$ python -m timeit -s "import test; d = test.get_data(1000)"
"test.sortcmp(d)"
10 loops, best of 3: 274 msec per loop

[D:\Steve]$ python -m timeit -s "import test; d = test.get_data(1000)"
"test.sortdsu(d)"
10 loops, best of 3: 131 msec per loop

# Requires 2.4

[D:\Steve]$ python -m timeit -s "import test; d = test.get_data(1000)"
"test.sortkey(d)"
10 loops, best of 3: 131 msec per loop

HTH,

STeVe
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by shalendra chhabra | last post: by
4 posts views Thread by dont bother | last post: by
7 posts views Thread by Federico G. Babelis | last post: by
19 posts views Thread by Owen T. Soroke | last post: by
1 post views Thread by john wright | last post: by
1 post views Thread by sdunkerson | last post: by
4 posts views Thread by =?Utf-8?B?TUdSaWRlb3V0?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.