gpt4 book ai didi

python - 获取带分数的zrange

转载 作者:可可西里 更新时间:2023-11-01 11:12:30 26 4
gpt4 key购买 nike

是否有可能获得元素的元组列表及其在 ZSET 中的分数?例如:

redis.zrange-function('channel', 0, -1)
[('item1', 123), ('item2', 333), etc.]

最佳答案

对于 redis-py , 在 zrange() 中有一个可选参数根据 docs for zrange() :

zrange(name, start, end, desc=False, withscores=False, score_cast_func=<type 'float'>)

Return a range of values from sorted set name between start and end sorted in ascending order.

...

withscores indicates to return the scores along with the values. The return type is a list of (value, score) pairs

例子:

In [292]: import redis

In [293]: r = redis.Redis()

In [294]: r.zadd('channel', 'a', 0, 'b', 5, 'c', 8, 'd', 20)
Out[294]: 4

In [295]: r.zrange('channel', 0, -1, withscores=True)
Out[295]: [(b'a', 0.0), (b'b', 5.0), (b'c', 8.0), (b'd', 20.0)]

关于python - 获取带分数的zrange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52582614/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com