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"
Python-Redis的Set操作的更多相关文章
- python之redis和memcache操作
Redis 教程 Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据 ...
- python对redis的常用操作 上 (对列表、字符串、散列结构操作)
这里的一切讨论均基于python的redis-py库. 安装使用: pip install redis 然后去获取一个redis客户端: redis_conn = redis.Redis(host=R ...
- Python redis 简单介绍
Python redis 简单介绍 1.安装 终端输入: pip(or)pip3.6 install redis 安装成功 2.哈哈,发现我并没有redis服务可以访问,所以到这里,在本机安装了red ...
- 生产消费者模式与python+redis实例运用(基础篇)
根据这个图,我们举个简单的例子:假如你去某个餐厅吃饭,点了很多菜,厨师要一个一个菜的做,一个厨师不可能同时做出所有你点的菜,于是你有两个选择:第一个,厨师把所有菜都上齐了,你才开始吃:还有一个选择,做 ...
- python -- redis连接与使用
前面我们简单介绍了redis nosql数据库,现在我们在python里面来使用redis. 一.python连接redis 在python中,要操作redis,目前主要是通过一个python-red ...
- windows中实现python,redis服务自动重启(任务计划程序+bat脚本)
需求:银行电脑无法自动开机,只能 通过 应用相关服务每天自动重启的方式实现 服务更新并且防止服务假死,内存过大 等情况 相关工具:win10系统中,使用windows自带的任务计划程序 和 bat脚本 ...
- python redis分布式锁改进
0X01 python redis分布式锁通用方法 REDIS分布式锁实现的方式:SETNX + GETSET 使用Redis SETNX 命令实现分布式锁 python 版本实现上述思路(案例1) ...
- python redis之连接池的原理
python redis之连接池的原理 转载地址 什么是连接池 通常情况下, 当我们需要做redis操作时, 会创建一个连接, 并基于这个连接进行redis操作, 操作完成后, 释放连接, 一般情况下 ...
- Redis数据类型及其操作
redis数据类型即操作 1. 字符串 set 设置字符串 格式: set key value 例子: set name kainhuck get 获取字符串的值 格式: get key 例子: ge ...
- redis的一些操作
public class WnsRedisFactory { private static Cache pool = null; private static JedisConnectionFacto ...
随机推荐
- 1.搭建Django开发环境
1.安装python(版本3.5.1) 官网下载:https://www.python.org/downloads/release/python-351/2.更新pip 命令:python -m pi ...
- STM32L0 复位和时钟控制 Reset and clock control (RCC)
时钟源: HSE:外部时钟 HSI16:可以直接用于系统时钟或者作为PLL输入.一般是1%精度 HSI48:The HSI48 clock signal is generated from an in ...
- Shiro:学习笔记(1)——身份验证
Shiro——学习笔记(1) 1.核心概念 1.Shiro不会自己去维护用户.维护权限:这些需要我们自己去设计/提供:然后通过相应的接口注入给Shiro.2.应用代码直接交互的对象是Subject,也 ...
- weak 的内部实现原理
问题 weak 变量在引用计数为0时,会被自动设置成 nil,这个特性是如何实现的? 答案 在 Friday QA 上,有一期专门介绍 weak 的实现原理.https://mikeash.com/p ...
- 2个canvas叠加运用(时钟例子)
最近在学习canvas,http://corehtml5canvas.com/code-live/,主要的学习方式就是通过上面的一些例子来学习canvas的一些用法.但是我发现,这里的例子,只要can ...
- 牛客练习赛13 B 幸运数字Ⅱ 【暴力】【二分】
题目链接 https://www.nowcoder.com/acm/contest/70/B 思路 没有代码限制 先打表 打出 幸运数字的表 然后 二分查找 第一个 大于 r 的幸运数字 然后 往 L ...
- js实现select动态添加option
关于 select 的添加 option 应该注意的问题. 标准的做法如上也就是说,标准的做法是 s.options.add();但是如果你一定要用 s.appendChild(option);注意了 ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- jQuery图片水平滑动延迟加载动画
在线演示 本地下载
- server.xml笔记
本文总结自: http://www.importnew.com/26156.html 核心元素: 顶层元素: server service 连接器: connector 容器: engine > ...