简体   繁体   中英

Get the last item in hash from redis using python

Using redis hgetall , gets all items as a dict.

How do i get the last five items in hash? Would like not to process the entire dict if possible.

dt = cxn.hgetall(k)

lt =[[key, dt[key]]for key in sorted(dt.iterkeys()) ] 

Above does not give me a clean solution, have to process lt again. Using python 2.7

EDIT: hgetall(k) return {'21': 'fooo', '2': 'bar' }, notice the key is string.

>>> dic={'1':100,'2':200,'3':300,'4':400,'5':500,'6':600,'7':700}

>>> print [[str(key), dic[str(key)]]for key in sorted(map(int,dic.iterkeys()))[-5:] ]
[['3', 300], ['4', 400], ['5', 500], ['6', 600], ['7', 700]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM