zoukankan      html  css  js  c++  java
  • Python-Redis的Set操作

    集合为不重复的列表

    无序集合

    sadd(name,values):在name对应的集合中添加元素

    smembers(name):获取name对应的集合的所有成员

    127.0.0.1:6379> sadd name john jack jack andy
    (integer) 3
    127.0.0.1:6379> smembers name
    1) "andy"
    2) "john"
    3) "jack"
    

    scard(name):获取name对应的集合中元素个数

    127.0.0.1:6379> smembers name
    1) "andy"
    2) "john"
    3) "jack"
    127.0.0.1:6379> scard name
    (integer) 3
    

    sdiff(keys, *args):在第一个name对应的集合中且不在其他name对应的集合的元素集合

    127.0.0.1:6379> smembers name
    1) "andy"
    2) "john"
    3) "css"
    4) "jack"
    5) "php"
    127.0.0.1:6379> smembers web
    1) "php"
    2) "javascript"
    3) "css"
    4) "html"
    127.0.0.1:6379> sdiff name web
    1) "andy"
    2) "john"
    3) "jack"
    

    sdiffstore(dest, keys, *args):获取第一个name对应的集合中且不在其他name对应的集合,再将其添加到dest对应的集合中

    127.0.0.1:6379> smembers name
    1) "andy"
    2) "john"
    3) "css"
    4) "jack"
    5) "php"
    127.0.0.1:6379> smembers web
    1) "php"
    2) "javascript"
    3) "css"
    4) "html"
    127.0.0.1:6379> sdiffstore name_web name web
    (integer) 3
    127.0.0.1:6379> smembers name_web
    1) "andy"
    2) "john"
    3) "jack"
    

    sismember(name, value):检查value是否是name对应的集合的成员

    127.0.0.1:6379> smembers web
    1) "php"
    2) "javascript"
    3) "css"
    4) "html"
    127.0.0.1:6379> sismember web asp
    (integer) 0
    127.0.0.1:6379> sismember web php
    (integer) 1
    

    smove(src, dst, value):将某个成员从一个集合中移动到另外一个集合,src为被移动成员的集合,移动成员到dst集合

    127.0.0.1:6379> smembers name
    1) "andy"
    2) "john"
    3) "jack"
    127.0.0.1:6379> smembers web
    1) "php"
    2) "javascript"
    3) "css"
    4) "html"
    127.0.0.1:6379> smove web name php
    (integer) 1
    127.0.0.1:6379> smembers name
    1) "php"
    2) "andy"
    3) "john"
    4) "jack"
    127.0.0.1:6379> smembers web
    1) "javascript"
    2) "css"
    3) "html"
    

    spop(name):从集合的右侧(尾部)返回并移除一个成员

    127.0.0.1:6379> smembers name
    1) "php"
    2) "andy"
    3) "john"
    4) "jack"
    127.0.0.1:6379> spop name
    "jack"
    127.0.0.1:6379> smembers name
    1) "php"
    2) "andy"
    3) "john"
    

    srandmember(name, numbers):从name对应的集合中随机获取 numbers 个元素

    127.0.0.1:6379> smembers web
    1) "php"
    2) "html"
    3) "css"
    4) "aspx"
    5) "asp"
    6) "ajax"
    7) "javascript"
    127.0.0.1:6379> srandmember web 2
    1) "ajax"
    2) "javascript"
    127.0.0.1:6379> srandmember web 5
    1) "html"
    2) "css"
    3) "aspx"
    4) "javascript"
    5) "ajax"
    

    srem(name, values):在name对应的集合中删除某些值

    127.0.0.1:6379> smembers web
    1) "php"
    2) "html"
    3) "css"
    4) "aspx"
    5) "asp"
    6) "ajax"
    7) "javascript"
    127.0.0.1:6379> srem web aspx
    (integer) 1
    127.0.0.1:6379> smembers web
    1) "php"
    2) "html"
    3) "css"
    4) "asp"
    5) "ajax"
    6) "javascript"
    

    sinter(keys, *args):获取name对应集合的并集

    sinterstore(dest, keys, *args):获取name对应集合的并集,并讲结果保存到dest对应的集合中

    127.0.0.1:6379> smembers name
    1) "jack"
    2) "andy"
    3) "php"
    4) "john"
    5) "jane"
    6) "html"
    7) "css"
    127.0.0.1:6379> smembers web
    1) "php"
    2) "html"
    3) "css"
    4) "asp"
    5) "ajax"
    6) "javascript"
    127.0.0.1:6379> sinter web name
    1) "php"
    2) "html"
    3) "css"
    127.0.0.1:6379> sinterstore web_name web name
    (integer) 3
    127.0.0.1:6379> smembers web_name
    1) "php"
    2) "css"
    3) "html"
    

    sunion(keys, *args):获取name对应的集合的并集

    sunionstore(dest,keys, *args):获取name对应的集合的并集,并将结果保存到dest对应的集合中

    127.0.0.1:6379> smembers name
    1) "andy"
    2) "php"
    3) "john"
    4) "jane"
    5) "html"
    6) "css"
    7) "jack"
    127.0.0.1:6379> smembers web
    1) "php"
    2) "html"
    3) "css"
    4) "asp"
    5) "ajax"
    6) "javascript"
    127.0.0.1:6379> sunion name web
     1) "john"
     2) "jane"
     3) "css"
     4) "html"
     5) "jack"
     6) "asp"
     7) "andy"
     8) "javascript"
     9) "ajax"
    10) "php"
    127.0.0.1:6379> sunionstore name_web name web
    (integer) 10
    127.0.0.1:6379> smembers name_web
     1) "john"
     2) "jane"
     3) "css"
     4) "html"
     5) "jack"
     6) "asp"
     7) "andy"
     8) "javascript"
     9) "ajax"
    10) "php"
    

    sscan(name, cursor=0, match=None, count=None):匹配name对应的集合中的value

    sscan_iter(name, match=None, count=None):为迭代匹配name对应的集合中的value

    127.0.0.1:6379> smembers name_web
     1) "john"
     2) "jane"
     3) "css"
     4) "html"
     5) "jack"
     6) "asp"
     7) "andy"
     8) "javascript"
     9) "ajax"
    10) "php"
    127.0.0.1:6379> sscan name_web 0  match j*
    1) "7"
    2) 1) "javascript"
       2) "john"
       3) "jane"
       4) "jack"
    

    有序集合

    有序集合中每一个元素有一个值和一个分数,分数专门用来排序

    zadd(name, *args, **kwargs):在name对应的有序集合中添加元素

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

    127.0.0.1:6379> zadd web 3 html 7 css 12 javascript 4 php
    (integer) 4
    127.0.0.1:6379> zrange web 0 -1 withscores
    1) "html"
    2) "3"
    3) "php"
    4) "4"
    5) "css"
    6) "7"
    7) "javascript"
    8) "12"
    127.0.0.1:6379> zrevrange web 0 -1 withscores
    1) "javascript"
    2) "12"
    3) "css"
    4) "7"
    5) "php"
    6) "4"
    7) "html"
    8) "3"
    

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

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

    127.0.0.1:6379> zadd web 3 html 7 css 12 javascript 4 php 15 asp 6 aspx
    (integer) 6
    127.0.0.1:6379> zrangebyscore web 0 5
    1) "html"
    2) "php"
    127.0.0.1:6379> zrevrangebyscore web 20 10
    1) "asp"
    2) "javascript"
    

    zcard(name):获取name对应的有序集合元素的数量

    127.0.0.1:6379> zadd web 3 html 7 css 12 javascript 4 php 15 asp 6 aspx
    (integer) 6
    127.0.0.1:6379> zcard web
    (integer) 6
    

    zcount(name, min, max):获取name对应的有序集合中分数在min到max之间的个数

    127.0.0.1:6379> zadd web 3 html 7 css 12 javascript 4 php 15 asp 6 aspx
    (integer) 6
    127.0.0.1:6379> zcount web 10 20
    (integer) 2
    

     zincrby(name, amount, value):自增name对应的有序集合中value对应的分数

    127.0.0.1:6379> zrange web 0 -1 withscores
     1) "html"
     2) "3"
     3) "php"
     4) "4"
     5) "aspx"
     6) "6"
     7) "css"
     8) "7"
     9) "javascript"
    10) "12"
    11) "asp"
    12) "15"
    127.0.0.1:6379> zincrby web 2 css
    "9"
    127.0.0.1:6379> zrange web 0 -1 withscores
     1) "html"
     2) "3"
     3) "php"
     4) "4"
     5) "aspx"
     6) "6"
     7) "css"
     8) "9"
     9) "javascript"
    10) "12"
    11) "asp"
    12) "15"
    

    zrank(name, value):获取name对应的有序集合中value的排行,从小到大排序,第一位为0

    zrevrank(name, value):获取name对应的有序集合中value的排行,从大到小排序,第一位为0

    127.0.0.1:6379> zrange web 0 -1 withscores
     1) "html"
     2) "3"
     3) "php"
     4) "4"
     5) "aspx"
     6) "6"
     7) "css"
     8) "9"
     9) "javascript"
    10) "12"
    11) "asp"
    12) "15"
    127.0.0.1:6379> zrank web php
    (integer) 1
    127.0.0.1:6379> zrevrank web php
    (integer) 4
    

    zrem(name, values):删除name对应的有序集合中的values

    127.0.0.1:6379> zrange web 0 -1 withscores
     1) "html"
     2) "3"
     3) "php"
     4) "4"
     5) "aspx"
     6) "6"
     7) "css"
     8) "9"
     9) "javascript"
    10) "12"
    11) "asp"
    12) "15"
    127.0.0.1:6379> zrem web aspx css
    (integer) 2
    127.0.0.1:6379> zrange web 0 -1 withscores
    1) "html"
    2) "3"
    3) "php"
    4) "4"
    5) "javascript"
    6) "12"
    7) "asp"
    8) "15"
    

    zremrangebyrank(name, min, max): 根据排行范围删除

    127.0.0.1:6379> zrange web 0 -1 withscores
     1) "html"
     2) "3"
     3) "php"
     4) "4"
     5) "aspx"
     6) "6"
     7) "css"
     8) "7"
     9) "javascript"
    10) "12"
    11) "asp"
    12) "15"
    127.0.0.1:6379> zremrangebyrank web 0 2
    (integer) 3
    127.0.0.1:6379> zrange web 0 -1 withscores
    1) "css"
    2) "7"
    3) "javascript"
    4) "12"
    5) "asp"
    6) "15"
    

    zremrangebyscore(name, min, max): 根据分数范围删除

    127.0.0.1:6379> zrange web 0 -1 withscores
     1) "html"
     2) "3"
     3) "php"
     4) "4"
     5) "aspx"
     6) "6"
     7) "css"
     8) "7"
     9) "javascript"
    10) "12"
    11) "asp"
    12) "15"
    127.0.0.1:6379> zremrangebyscore web 0 5
    (integer) 2
    127.0.0.1:6379> zrange web 0 -1 withscores
    1) "aspx"
    2) "6"
    3) "css"
    4) "7"
    5) "javascript"
    6) "12"
    7) "asp"
    8) "15"
    

    zscore(name, value):获取name对应的有序集合中value对应的分数

    127.0.0.1:6379> zrange web 0 -1 withscores
    1) "aspx"
    2) "6"
    3) "css"
    4) "7"
    5) "javascript"
    6) "12"
    7) "asp"
    8) "15"
    127.0.0.1:6379> zscore web css
    "7"
    

    zinterstore(dest, number, keys, aggregate=None):获取number个有序集合的交集,并存入到dest集合中,如果遇到相同值不同分数,则按照aggregate进行操作,aggregate的值为:SUM,MIN,MAX

    127.0.0.1:6379> zrange web 0 -1 withscores
     1) "html"
     2) "3"
     3) "php"
     4) "4"
     5) "aspx"
     6) "6"
     7) "css"
     8) "7"
     9) "javascript"
    10) "12"
    11) "asp"
    12) "15"
    127.0.0.1:6379> zrange name 0 -1 withscores
     1) "john"
     2) "3"
     3) "jane"
     4) "7"
     5) "php"
     6) "9"
     7) "jack"
     8) "12"
     9) "andy"
    10) "17"
    11) "css"
    12) "32"
    127.0.0.1:6379> zinterstore web_name 2 web name
    (integer) 2
    127.0.0.1:6379> zrange web_name 0 -1 withscores
    1) "php"
    2) "13"
    3) "css"
    4) "39"
    

    zunionstore(dest, number, keys, aggregate=None):获取number个有序集合的并集,如果遇到相同值不同分数,则按照aggregate进行操作,aggregate的值为:SUM,MIN,MAX

    127.0.0.1:6379> zrange web 0 -1 withscores
     1) "html"
     2) "3"
     3) "php"
     4) "4"
     5) "aspx"
     6) "6"
     7) "css"
     8) "7"
     9) "javascript"
    10) "12"
    11) "asp"
    12) "15"
    127.0.0.1:6379> zrange name 0 -1 withscores
     1) "john"
     2) "3"
     3) "jane"
     4) "7"
     5) "php"
     6) "9"
     7) "jack"
     8) "12"
     9) "andy"
    10) "17"
    11) "css"
    12) "32"
    127.0.0.1:6379> zunionstore web_name 2 web name
    (integer) 10
    127.0.0.1:6379> zrange web_name 0 -1 withscores
     1) "html"
     2) "3"
     3) "john"
     4) "3"
     5) "aspx"
     6) "6"
     7) "jane"
     8) "7"
     9) "jack"
    10) "12"
    11) "javascript"
    12) "12"
    13) "php"
    14) "13"
    15) "asp"
    16) "15"
    17) "andy"
    18) "17"
    19) "css"
    20) "39"
    

     zscan(name, cursor=0, match=None, count=None, score_cast_func=float):匹配name对应的有序集合中的value

    zscan_iter(name, match=None, count=None,score_cast_func=float):迭代匹配

    127.0.0.1:6379> zrange web 0 -1 withscores
     1) "html"
     2) "3"
     3) "php"
     4) "4"
     5) "aspx"
     6) "6"
     7) "css"
     8) "7"
     9) "javascript"
    10) "12"
    11) "asp"
    12) "15"
    127.0.0.1:6379> zscan web 0 match *a*
    1) "0"
    2) 1) "aspx"
       2) "6"
       3) "javascript"
       4) "12"
       5) "asp"
       6) "15"
    
  • 相关阅读:
    印象笔记和有道云笔记竞品分析
    印象笔记需求分析文档
    ES(Elasticsearch)
    java8中optional和.stream().map()
    设计模式-builder(构造器模式)
    throw与throws
    异常java.lang.NumberFormatException解决
    Spring注解
    Spring配置数据源以及hibernate
    log4j配置文件——hibernate
  • 原文地址:https://www.cnblogs.com/sch01ar/p/8474627.html
Copyright © 2011-2022 走看看