使用Python读写Redis——Sets

发布时间:2024年01月21日

之前详细介绍了 Redis命令 - Sets命令组常用命令,同样的命令,本文将用python调用redis库封装好的方法。

要操作的Sets命令如下

1、SADD key member [member …]
2、SCARD key
3、SMEMBERS key
4、SRANDMEMBER key [count]
5、SPOP key [count]
6、SMOVE source destination member
7、SINTER key [key …]
8、SUNION key [key …]
9、SDIFF key [key …]

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使用Sets命令

myset = "myset"

# SADD key member [member …]
# 若key不存在会创建key
# case1:元素不存在,设置成功,返回受影响的成员数
print(r.sadd(myset, 0))  # 1
# case2: 元素已存在,返回:0
print(r.sadd(myset, 0))  # 0
print(r.sadd(myset, *[1, 2, 3]))  # 3

# SCARD key
print(r.scard(myset))  # 返回集合的数量:4

# SMEMBERS key
# 不会删除成员
print(r.smembers(myset))  # 查看集合所有成员:{b'0', b'3', b'2', b'1'}

# SRANDMEMBER key [count]
# 随机返回集合中的count个元素,不会删除成员。count可以是负数
# case1: count默认是1,随机返回1个成员:
print(r.srandmember(myset))  # b'1'
# case2: 随机返回2个成员:
print(r.srandmember(myset, 2))  # [b'1', b'3']
# case3: count是正整数,且超出集合长度,返回整个集合的所有元素。注意跟case5对比
print(r.srandmember(myset, 6))  # [b'0', b'1', b'2', b'3']
# case4: count可以是负数,取count的绝对值。随机返回3个成员:
print(r.srandmember(myset, -3))  # [b'0', b'2', b'2']
# case5: count是负数,且绝对值超出集合长度,返回结果集中会存在一个元素多次出现的情况
print(r.srandmember(myset, -6))  # [b'0', b'1', b'0', b'3', b'2', b'2']
print(r.smembers(myset))  # {b'0', b'3', b'2', b'1'}
# case6: key不存在
print(r.srandmember("myset1"))  # None

# SPOP key [count]
# 返回count个随机元素,会删除元素。count不能为负数
print(r.spop(myset))  # count默认是1。返回值:b'2'
print(r.spop(myset, 2))  # [b'3', b'0']
print(r.smembers(myset))  # {b'1'}

# SMOVE source destination member
# Step1:先创建两个集合
print(r.sadd("myset1", *[0, 1, 2]))  # 3
print(r.sadd("myset2", *[1, 2, 3]))  # 3

# case1:source集合不存在
print(r.smove("myset3", "myset2", "1"))  # False
# case2:source集合不包含指定的元素
print(r.smove("myset1", "myset2", "4"))  # False
print(r.smembers("myset1"))  # {b'0', b'2', b'1'}
print(r.smembers("myset2"))  # {b'3', b'2', b'1'}

# case3:source 集合存在且包含指定的元素,且目标集合中无此元素,将把此元素从myset1移除,挪至myset2
print(r.smove("myset1", "myset2", "0"))  # True
print(r.smembers("myset1"))  # {b'2', b'1'}
print(r.smembers("myset2"))  # {b'0', b'3', b'2', b'1'}

# case4:source 集合存在且包含指定的元素,但目标集合中已存在此元素,仅将此元素从myset1移除
print(r.smove("myset1", "myset2", "1"))  # True
print(r.smembers("myset1"))  # {b'2'}
print(r.smembers("myset2"))  # {b'0', b'3', b'2', b'1'}

# case5: 若source和destination不是集合类型。会报exception:WRONGTYPE Operation against a key holding the wrong kind of value
# print(r.smove("myhash", "myset2", "1"))

# SINTER key [key …]
# 求多个集合的交集
print(r.sadd("key1", *["1", "2", "3", "4"]))  # 4
print(r.sadd("key2", *["3"]))  # 1
print(r.sadd("key3", *["1", "3", "5"]))  # 3
# case1: 求交集,如果key都存在
print(r.sinter(["key1", "key2", "key3"]))  # {b'3'}
# case2: 求交集,如果key不存在
print(r.sinter(["key4", "key2", "key3"]))  # set(), 空集合

# SUNION key [key …]
# 求多个集合的并集
print(r.sunion(["key1", "key2", "key3"]))  # {b'1', b'5', b'4', b'2', b'3'}
# key4不存在,会被当成空集
print(r.sunion(["key1", "key2", "key4"]))  # {b'1', b'3', b'2', b'4'}

# SDIFF key [key …]
# 求多个集合的差集,最后一个key跟前面的keys的比较
print(r.sdiff(["key1", "key2", "key3"]))  # {b'4', b'2'}
print(r.sdiff(["key1", "key2"]))  # {b'4', b'2', b'1'}
print(r.sdiff(["key1", "key3"]))  # {b'4', b'2'}
print(r.sdiff(["key2", "key3"]))  # set(), 空集合
文章来源:https://blog.csdn.net/weixin_44691253/article/details/135633136
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。