簡體   English   中英

使用python從Redis獲取哈希中的最后一項

[英]Get the last item in hash from redis using python

使用redis hgetall ,將所有項目作為字典。

我如何獲得哈希中的最后五個項目? 如果可能,不要處理整個字典。

dt = cxn.hgetall(k)

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

上面並沒有給我一個干凈的解決方案,必須處理lt一次。 使用python 2.7

編輯: hgetall(k) return {'21': 'fooo', '2': 'bar' },注意關鍵是字符串。

>>> 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]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM