之前详细介绍了 Redis命令 - Zsets命令组常用命令,同样的命令,本文将用python调用redis库封装好的方法。文章来源:https://www.toymoban.com/news/detail-817346.html
要操作的Zsets命令如下
1、ZADD key score member [score] [member]
2、ZRANGE key start stop [WITHSCORES]
3、ZCARD key
4、ZRANK key member
5、ZSCORE key member
6、ZCOUNT key min max
7、ZINCRBY key increment member
8、ZREVRANGE key start stop [WITHSCORES]
9、ZREVRANGEBYSCORE key max min [WITHSCORES]
10、ZREVRANK key member
11、ZREM key member [member …]文章来源地址https://www.toymoban.com/news/detail-817346.html
Python连接redis
import redis
from config.VAR import INT_REDIS_HOST, INT_REDIS_PORT, INT_REDIS_PASSWORD
# 便于修改,将redis的连接配置在VAR.py中,管理这些常量
r = redis.StrictRedis(host=INT_REDIS_HOST, port=INT_REDIS_PORT, password=INT_REDIS_PA
Python使用Zsets命令
zset = "myzset"
dict = {"a": 100, "b": 200, "c": 300}
# ZADD key score member [score] [member]
# 新创建一个zset,有3个成员,返回值为key-value的键值对个数
print(r.zadd(zset, dict)) # 3
# 成员已存在,且score也相同,返回0
print(r.zadd(zset, {"a": 100})) # 0
# 成员已存在,更新score值,返回
print(r.zadd(zset, {"a": 400})) # 0
# 成员不存在的时候添加新成员
print(r.zadd(zset, {"d": 500}, nx=True)) # 1
print(r.zadd(zset, {"e": 700}, xx=False)) # 1
# ZRANGE key start stop [WITHSCORES]
# case1: 带score
print(r.zrange(zset, 0, -1)) # [b'b', b'c', b'a', b'd', b'e']
# case2: 不带score
print(r.zrange(zset, 0, -1,
withscores=True)) # [(b'b', 200.0), (b'c', 300.0), (b'a', 400.0), (b'd', 500.0), (b'e', 700.0)]
# ZCARD key
# 获取有序集合的成员数
# case1:key存在
print(r.zcard(zset)) # 5
# case2:key不存在, 返回0
print(r.zcard("myzset1")) # 0
# ZRANK key member
# 返回有序集合中指定成员的索引(按照score排序后的, 且从0开始)
print(r.zrange(zset, 0, -1,
withscores=True)) # [(b'b', 200.0), (b'c', 300.0), (b'a', 400.0), (b'd', 500.0), (b'e', 700.0)]
print("c: ", r.zrank(zset, "c")) # 1
print("d: ", r.zrank(zset, "d")) # 3
print("e: ", r.zrank(zset, "e")) # 4
# ZSCORE key member
# 返回有序集中,成员的分数值
print(r.zscore(zset, "c")) # 300.0
# ZCOUNT key min max
# 计算在有序集合中指定区间分数的成员数【min, max】闭区间
print(r.zcount(zset, 300, 500)) # 3
# ZINCRBY key increment member
# 有序集合中对指定成员的分数加上增量 increment
print(r.zincrby(zset, 100, "c")) # 400.0
# ZREVRANGE key start stop [WITHSCORES]
# 返回有序集中指定区间内的成员,通过索引,分数从高到低
print(r.zrevrange(zset, 0, -1,
withscores=True)) # [(b'e', 700.0), (b'd', 500.0), (b'c', 400.0), (b'a', 400.0), (b'b', 200.0)]
# ZREVRANGEBYSCORE key max min [WITHSCORES]
# 返回有序集中指定分数区间内的成员,分数从高到低排序
print(r.zrevrangebyscore(zset, 600, 400, withscores=True)) # [(b'd', 500.0), (b'c', 400.0), (b'a', 400.0)]
# ZREVRANK key member
# 返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序
print(r.zrange(zset, 0, -1,
withscores=True)) # [(b'b', 200.0), (b'a', 400.0), (b'c', 400.0), (b'd', 500.0), (b'e', 700.0)]
print("a: ", r.zrevrank(zset, "a")) # 3
print("b: ", r.zrevrank(zset, "b")) # 4
print("c: ", r.zrevrank(zset, "c")) # 2
print("d: ", r.zrevrank(zset, "d")) # 1
print("e: ", r.zrevrank(zset, "e")) # 0
# ZREM key member [member …]
# 移除有序集合中的一个或多个成员
print(r.zrange(zset, 0, -1,
withscores=True)) # [(b'b', 200.0), (b'a', 400.0), (b'c', 400.0), (b'd', 500.0), (b'e', 700.0)]
print(r.zrem(zset, *["a", "b"])) # 2
print(r.zrange(zset, 0, -1, withscores=True)) # [(b'c', 400.0), (b'd', 500.0), (b'e', 700.0)]
到了这里,关于使用Python读写Redis——Zsets的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!