简体   繁体   English

如何使用zscan以相反的顺序遍历Redis中的有序集?

[英]How to traverse a sorted set in Redis in reverse order using zscan?

I have a sorted set in Redis with priorities starting from 0 up to 3. I would like to traverse this sorted set from highest to lowest priority using the python iterator zscan_iter . 我在Redis中有一个排序集,优先级从0到3.我想使用python iterator zscan_iter从最高优先级到最低优先级遍历这个有序集。 However, using zscan_iter gives me the items starting from 0. Is there a way to reverse the order? 但是,使用zscan_iter给我从0开始的项目。有没有办法扭转顺序? Unfortunately, reverse() only works on iterators and not on python generators. 不幸的是, reverse()只适用于迭代器而不适用于python生成器。

I see two solutions: 我看到两个解决方案:

  1. Use negative priorities (so instead of 3 use -3) 使用负优先级(所以而不是3使用-3)
  2. Paginate through slices of keys using ZREVRANGEBYSCORE , however I would prefer to use an iterator. 使用ZREVRANGEBYSCORE通过切片键分页,但我更喜欢使用迭代器。

Are there any other ways of doing this? 有没有其他方法可以做到这一点?

Scanning the Sorted Set with an iterator does not guarantee any order. 使用迭代器扫描排序集并不保证任何订单。 Use ZREVRANGEBYSCORE for that. 使用ZREVRANGEBYSCORE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按排序顺序遍历字典键 - How to traverse dictionary keys in sorted order 如何在网页中显示Redis排序集合元素 - How to show Redis sorted set elements in a webpage 无法使用python在redis中使用ZADD(排序集)插入数据 - not able to insert data using ZADD(sorted set ) in redis using python 在Redis中使用排序集的替代数据模型(对于Django / Python项目) - Alternative data model to using sorted set in redis (for a Django/Python project) 如何按等级执行排序集的 Redis 客户端缓存 - How to perform Redis client side caching of a Sorted Set, by rank 如何在Python中以相反的顺序遍历列表(索引样式:“ ...仅在range(...)”) - How to traverse a list in reverse order in Python (index-style: '… in range(…)' only) 如何识别ndb查询已排序,以及如何在不知道排序顺序的情况下反转排序顺序? - How to recognize that ndb query is sorted and how to reverse sort order without knowledge of sort order? 哨兵值排序顺序和反向排序 - Sentinel Values sorted order and reverse sort 为什么 Redis 'Zset' 的意思是 'Sorted Set'? - Why Redis 'Zset' means 'Sorted Set'? 如何使用 sorted() 函数以逆字母顺序打印列表? (Python) - How can you use the sorted() function to print a list in reverse alphabetical order? (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM