前文写了redis的第五种数据结构zset,这篇文章照例用python实现一下。(其它几种数据结构,python客户端和redis客户端的语法还比较一致的,但是zset的一些差别是真的有点大,强烈建议结合前文redis客户端一起阅读)
from redis import Redis
if __name__ == '__main__':
client = Redis(host='192.168.174.129', port=6666, decode_responses=True)
try:
# 添加member Linux客户端是把score写在member前面,而python客户端的key是member,value是score
print(client.zadd('z1', {'tom': 77, 'mike': 90, 'michael': 60})) # 3
# 添加的member已存在,且score也相同:
print(client.zadd('z1', {'tom': 77})) # 0
# 添加score不同,但member已存在:
print(client.zadd('z1', {'tom': 88})) # 0
# 添加的member不存在,score也已存在:
print(client.zadd('z1', {'tony': 60})) # 1
# ch nx xx incr
print(client.zadd('z1', {'tom': 87}, ch=True)) # 1
print(client.zadd('z1', {'tom': 11}, nx=True, ch=True)) # 0
print(client.zadd('z1', {'jerry': 22}, xx=True, ch=True)) # 0
print(client.zadd('z1', {'tony': 6}, ch=True, incr=True)) # 66.0
# 查看所有members (默认按score升序)
print(client.zrange('z1', 0, -1)) # ['michael', 'tony', 'tom', 'mike']
# python客户端加一个desc参数就能实现Linux客户端zrevrange的功能
print(client.zrange('z1', 0, -1, desc=True)) # ['mike', 'tom', 'tony', 'michael']
print(client.zrange('z1', 0, -1, withscores=True))
# [('michael', 60.0), ('tony', 66.0), ('tom', 87.0), ('mike', 90.0)]
# python客户端与Linux客户端的第二个不同点是: 可以对score进一步加工 再返回
print(client.zrange('z1', 0, -1, withscores=True, desc=True, score_cast_func=lambda x: 'score:' + str(x)))
# [('mike', 'score:90'), ('tom', 'score:87'), ('tony', 'score:66'), ('michael', 'score:60')]
# 倒叙查看,其它参数和zrange类似
print(client.zrevrange('z1', 0, -1)) # ['mike', 'tom', 'tony', 'michael']
# 排名(score升序)
print(client.zrank('z1', 'tom')) # 2
# (倒序)
print(client.zrevrank('z1', 'tom')) # 1
# 查看指定score范围内的元素(升序排列)
print(client.zrangebyscore('z1', 60, 87)) # ['michael', 'tony', 'tom']
print(client.zrangebyscore('z1', 60, 87, withscores=True))
# [('michael', 60.0), ('tony', 66.0), ('tom', 87.0)]
# 偏移1位,取1位 :
print(client.zrangebyscore('z1', 60, 87, 1, 1, withscores=True)) # [('tony', 66.0)]
# 查看指定score范围内的元素(降序排列)
print(client.zrevrangebyscore('z1', 87, 60)) # ['tom', 'tony', 'michael']
# 查看有序集合内元素个数
print(client.zcard('z1')) # 4
# 查看有序集合内 member的score
print(client.zscore('z1', 'tony')) # 66.0
# 增加member的score
print(client.zincrby('z1', 10, 'tony')) # 76.0
# 查看指定score范围内的元素个数
print(client.zcount('z1', 70, 90)) # 3
# 删除指定元素
print(client.zrem('z1', 'tony')) # 1
# 删除指定排名内的元素 (min, max 相同就只删除一个)
print(client.zremrangebyrank('z1', -1, -1)) # 1
print(client.zrange('z1', 0, -1)) # ['michael', 'tom']
# 删除指定score范围内的元素
print(client.zremrangebyscore('z1', 80, 90)) # 1
print(client.zrange('z1', 0, -1)) # ['michael']
# zinterstore
# 造一些数据,三个同学的半期考试成绩:
mid_test = {
'Han Meimei': 70,
'Li Lei': 70,
'tom': 99.5
}
fin_test = {
'Han Meimei': 75,
'Li Lei': 88,
'tom': 99.5
}
client.zadd('mid_test', mid_test)
client.zadd('fin_test', fin_test)
# 半期考试成绩和期末考试成绩相加
print(client.zinterstore('sumstore1', ['mid_test', 'fin_test'])) # 3
print(client.zrange('sumstore1', 0, -1, withscores=True))
# [('Han Meimei', 145.0), ('Li Lei', 158.0), ('tom', 199.0)]
# 期末考试的占比更大,将期末成绩*2再加上半期成绩:
print(client.zinterstore('sumstore2', {'mid_test': 1, 'fin_test': 2})) # 3
print(client.zrange('sumstore2', 0, -1, withscores=True))
# [('Han Meimei', 220.0), ('Li Lei', 246.0), ('tom', 298.5)]
# 查看他们2次考试的最高分:
print(client.zinterstore('maxstore', ['mid_test', 'fin_test'], aggregate='max')) # 3
print(client.zrange('maxstore', 0, -1, withscores=True))
# [('Han Meimei', 75.0), ('Li Lei', 88.0), ('tom', 99.5)]
# zunionstore和zinterstore 的用法类似,建议参照上面和前一篇文章
# zrangebylex
client.zadd('myzset', {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0})
print(client.zrangebylex('myzset', '-', '[c')) # ['a', 'b', 'c']
print(client.zrangebylex('myzset', '-', '(c')) # ['a', 'b']
print(client.zrangebylex('myzset', '[aaa', '(g'))
# ['b', 'c', 'd', 'e', 'f']
# zlexcount,zremrangebylex用法和zrangebylex类似 强烈建议结合上一篇文章阅读
except Exception as e:
print(e)
finally:
client.close()