Python-- Redis Set

來源:互聯網
上載者:User

標籤:資料   第一個   highlight   mat   style   err   gpo   inf   fst   

一、無序集合

  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‘))#輸出TrueFalse

  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分數增加 amountprint(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)]21

  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 預設 SUMprint(‘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,用來對分數進行操作

 

Python-- Redis Set

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.