Python-- Redis Set

一、无序集合

  Set操作,Set集合就是不允许重复的列表

  1.1 sadd(name, values)

# name对应的集合中添加元素

  1.2 smembers(name)

# 获取name对应的集合的所有成员
r.sadd('s1', 't1', 't2', 't3', 't1')
print(r.smembers('s1'))

# 输出
{b't1', b't2', b't3'}

# 集合是去重的

  1.3 scard(name)

#获取name对应的集合中元素个数

print(r.scard('s1'))

#输出
3

  1.4 sdiff(keys, *args)

# 在第一个name对应的集合中且不在其他name对应的集合的元素集合

print(r.smembers('s1'))
print(r.smembers('s2'))
print(r.sdiff('s1', 's2'))

#输出
{b't3', b't1', b't2'}
{b't1', b't5', b't4'}
{b't3', b't2'}

# 集合s1中的 t2 和 t3 不在s2中 

  1.5 sdiffstore(dest, keys, *args)

# 获取第一个name对应的集合中且不在其他name对应的集合,
#再将其新加入到dest对应的集合中

print('s1:', r.smembers('s1'))
print('s2:', r.smembers('s2'))
r.sdiffstore('s3', 's1', 's2')
print('s3:', r.smembers('s3'))

#输出
s1: {b't1', b't3', b't2'}
s2: {b't4', b't5', b't1'}
s3: {b't3', b't2'}

  1.6 sinter(keys, *args)

# 获取多个集合的交集

print('s1:', r.smembers('s1'))
print('s2:', r.smembers('s2'))
print('交集:', r.sinter('s1', 's2'))

#输出
s1: {b't2', b't1', b't3'}
s2: {b't1', b't4', b't5'}
交集: {b't1'}

  1.7 sinterstore(dest, keys, *args)

# 获取多一个name对应集合的并集,再讲其加入到dest对应的集合中

print('s1:', r.smembers('s1'))
print('s2:', r.smembers('s2'))
r.sinterstore('s4', 's1', 's2')
print('s4:', r.smembers('s4'))

#输出
s1: {b't3', b't2', b't1'}
s2: {b't1', b't5', b't4'}
s4: {b't1'}

  1.8 sismember(name, value)

# 检查value是否是name对应的集合的成员

print(r.sismember('s1', 't1'))
print(r.sismember('s1', 't5'))

#输出
True
False

  1.9 smove(src, dst, value)

# 将某个成员从一个集合中移动到另外一个集合

print('s1:', r.smembers('s1'))
print('s2:', r.smembers('s2'))
r.smove('s1', 's2', 't2')
print('s1:', r.smembers('s1'))
print('s2:', r.smembers('s2'))

# 输出
s1: {b't2', b't1', b't3'}
s2: {b't4', b't1', b't5'}
s1: {b't1', b't3'}
s2: {b't4', b't2', b't1', b't5'}

  1.10 spop(name)

# 从集合的右侧(尾部)移除一个成员,并将其返回

print('s1:', r.smembers('s1'))
r.spop('s1')
print('s1:', r.smembers('s1'))

#输出
s1: {b't1', b't3'}
s1: {b't1'}

  1.11 srandmember(name, numbers)

# 从name对应的集合中随机获取 numbers 个元素

print('s2:', r.smembers('s2'))
print(r.srandmember('s2', 3))

#输出,从s2中随机获取3个数
s2: {b't5', b't2', b't1', b't4'}
[b't5', b't2', b't1']

  1.12 srem(name, values)

# 在name对应的集合中删除某些值

print('s2:', r.smembers('s2'))
r.srem('s2', 't5')
print('s2:', r.smembers('s2'))

#输出
s2: {b't2', b't1', b't5', b't4'}
s2: {b't2', b't1', b't4'}

  1.13 sunion(keys, *args)

# 获取多一个name对应的集合的并集

print(r.smembers('s3'))
print(r.smembers('s4'))
print(r.sunion('s3', 's4'))

#输出
{b't3', b't2'}
{b't1'}
{b't1', b't3', b't2'}

  1.14 sunionstore(dest,keys, *args)

# 获取多一个name对应的集合的并集,并将结果保存到dest对应的集合中

print('s3:', r.smembers('s3'))
print('s4:', r.smembers('s4'))
r.sunionstore('s6', 's3', 's4')
print('s6:', r.smembers('s6'))

#输出
s3: {b't2', b't3'}
s4: {b't1'}
s6: {b't2', b't1', b't3'}

  1.15 sscan(name, cursor=0, match=None, count=None)

# 分片获取数据

print('test_info:', r.smembers('test_info'))
print(r.sscan('test_info', 0, match='J*'))


# 输出
test_info: {b'Jerry', b'Jack', b'Tom', b'Sam'}
(0, [b'Jack', b'Jerry'])

  1.16 sscan_iter(name, match=None, count=None)

# 同字符串的操作,用于增量迭代分批获取元素,避免内存消耗太大

  

二、有序集合

  有序集合,在集合的基础上,为每元素排序;元素的排序需要根据另外一个值来进行比较,所以,对于有序集合,每一个元素有两个值,即:值和分数,分数专门用来做排序。

  2.1 zadd(name, *args, **kwargs)

# 在name对应的有序集合中添加元素
# 如:
     # zadd('zz', 'n1', 1, 'n2', 2)
     # 或
     # zadd('zz', n1=11, n2=22)


r.zadd('z1', 't1', 10, 't2', 5, 't3', 4, 't4', 8)

  2.2 zrange( name, start, end, desc=False, withscores=False, score_cast_func=float)

# 按照索引范围获取name对应的有序集合的元素

 
# 参数:
    # name,redis的name
    # start,有序集合索引起始位置(非分数)
    # end,有序集合索引结束位置(非分数)
    # desc,排序规则,默认按照分数从小到大排序
    # withscores,是否获取元素的分数,默认只获取元素的值
    # score_cast_func,对分数进行数据转换的函数

# 更多:
    # 从大到小排序
    # zrevrange(name, start, end, withscores=False, score_cast_func=float)
 
    # 按照分数范围获取name对应的有序集合的元素
    # zrangebyscore(name, min, max, start=None, num=None, withscores=False, score_cast_func=float)
    # 从大到小排序
    # zrevrangebyscore(name, max, min, start=None, num=None, withscores=False, score_cast_func=float)


print(r.zrange('z1', 0, -1))
print(r.zrange('z1', 0, -1, withscores=True))

#输出
[b't3', b't2', b't4', b't1']
[(b't3', 4.0), (b't2', 5.0), (b't4', 8.0), (b't1', 10.0)]

  2.3 zcard(name)

# 获取name对应的有序集合元素的数量

print(r.zcard('z1'))

#输出
4

  2.4 zcount(name, min, max)

# 获取name对应的有序集合中分数 在 [min,max] 之间的个数

print(r.zrange('z1', 0, -1, withscores=True))

print(r.zcount('z1', 5, 8))

#输出
2

  2.5 zincrby(name, value, amount)

# name 对应的有序集合中的value分数增加 amount

print(r.zrange('z1', 0, -1, withscores=True))
print(r.zincrby('z1', 't3', 6))
print(r.zrange('z1', 0, -1, withscores=True))

#输出
[(b't3', 4.0), (b't2', 5.0), (b't4', 8.0), (b't1', 10.0)]
10.0
[(b't2', 5.0), (b't4', 8.0), (b't1', 10.0), (b't3', 10.0)]

  2.6 zrank(name, value)

# 获取某个值在 name对应的有序集合中的排行(从 0 开始)
 
# 更多:
    # zrevrank(name, value),从大到小排序

print(r.zrange('z1', 0, -1, withscores=True))
print(r.zrank('z1', 't1'))
print(r.zrevrank('z1', 't1'))

#输出
[(b't2', 5.0), (b't4', 8.0), (b't1', 10.0), (b't3', 10.0)]
2
1

  2.7 zrem(name, values)

# 删除name对应的有序集合中值是values的成员

# 如:zrem('zz', ['s1', 's2'])

print(r.zrange('z1', 0, -1, withscores=True))
r.zrem('z1', 't1')
print(r.zrange('z1', 0, -1, withscores=True))

#输出
[(b't2', 5.0), (b't4', 8.0), (b't1', 10.0), (b't3', 10.0)]
[(b't2', 5.0), (b't4', 8.0), (b't3', 10.0)

  2.8 zremrangebyrank(name, min, max)

# 根据排行范围删除,不在该范围内的都删除

r.zremrangebyrank('z1', 1, 6)

print(r.zrange('z1', 0, -1, withscores=True))

#输出


[(b't2', 5.0)]

  2.9 zremrangebyscore(name, min, max)

# 根据分数范围删除

print(r.zrange('z1', 0, -1, withscores=True))
r.zremrangebyscore('z1', 1, 6)
print(r.zrange('z1', 0, -1, withscores=True))

#输出
[(b't3', 4.0), (b't2', 5.0), (b't4', 8.0), (b't1', 10.0)]
[(b't4', 8.0), (b't1', 10.0)]

  2.10 zscore(name, value)

# 获取name对应有序集合中 value 对应的分数

print(r.zrange('z1', 0, -1, withscores=True))

print(r.zscore('z1', 't1'))

#输出
[(b't4', 8.0), (b't1', 10.0)]
10.0

  2.11 zinterstore(dest, keys, aggregate=None)

# 获取两个有序集合的交集,如果遇到相同值,则按照aggregate进行操作
# aggregate的值为:  SUM  MIN  MAX 默认 SUM

print('z2:', r.zrange('z2', 0, -1, withscores=True))
print('z3:', r.zrange('z3', 0, -1, withscores=True))
r.zinterstore('z6', {'z2', 'z3'})
print('z6:', r.zrange('z6', 0, -1, withscores=True))

# 输出
z2: [(b't3', 4.0), (b't2', 5.0), (b't4', 8.0), (b't1', 10.0)]
z3: [(b't3', 2.0), (b't1', 6.0), (b't2', 7.0), (b't4', 12.0)]
z6: [(b't3', 6.0), (b't2', 12.0), (b't1', 16.0), (b't4', 20.0)]
print('z2:', r.zrange('z2', 0, -1, withscores=True))
print('z3:', r.zrange('z3', 0, -1, withscores=True))
r.zinterstore('z7', {'z2', 'z3'}, aggregate='MIN')
print('z7:', r.zrange('z7', 0, -1, withscores=True))
r.zinterstore('z8', {'z2', 'z3'}, aggregate='MAX')
print('z8:', r.zrange('z8', 0, -1, withscores=True))

# 输出
z2: [(b't3', 4.0), (b't2', 5.0), (b't4', 8.0), (b't1', 10.0)]
z3: [(b't3', 2.0), (b't1', 6.0), (b't2', 7.0), (b't4', 12.0)]
z7: [(b't3', 2.0), (b't2', 5.0), (b't1', 6.0), (b't4', 8.0)]
z8: [(b't3', 4.0), (b't2', 7.0), (b't1', 10.0), (b't4', 12.0)]

  如果其中两个集合中个数不符合,则单独的那个值不会进行运算

  2.12 zunionstore(dest, keys, aggregate=None)

print('z2:', r.zrange('z2', 0, -1, withscores=True))
print('z3:', r.zrange('z3', 0, -1, withscores=True))
r.zunionstore('z10', {'z2', 'z3'})
print('z10:', r.zrange('z10', 0, -1, withscores=True))

#输出
z2: [(b't3', 4.0), (b't2', 5.0), (b't4', 8.0)]
z3: [(b't3', 2.0), (b't1', 6.0), (b't2', 7.0), (b't4', 12.0)]
z10: [(b't1', 6.0), (b't3', 6.0), (b't2', 12.0), (b't4', 20.0)]

  2.13 zscan(name, cursor=0, match=None, count=None, score_cast_func=float)

       2.14 zscan_iter(name, match=None, count=None,score_cast_func=float)

# 同字符串相似,相较于字符串新增score_cast_func,用来对分数进行操作

 

posted @ 2018-01-16 17:52  Bigberg  阅读(1101)  评论(0编辑  收藏  举报