Python操作redis系列--列表(list)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python操作redis系列--列表(list),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含36877字,纯文字阅读大概需要53分钟。
内容图文
![Python操作redis系列--列表(list)](/upload/InfoBanner/zyjiaocheng/866/cfec8c09487a4c1fa2f69cbefdcd2b7a.jpg)
<div id="cnblogs_post_body" class="blogpost-body">
<div class="cnblogs_code">
<pre># -*- coding: utf-<span style="color: rgba(128, 0, 128, 1)">8</span> -*-<span style="color: rgba(0, 0, 0, 1)">
import redis
r </span>=redis.Redis(host=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">123.156.74.190</span><span style="color: rgba(128, 0, 0, 1)">"</span>,port=<span style="color: rgba(128, 0, 128, 1)">6379</span>,password=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">66666666666</span><span style="color: rgba(128, 0, 0, 1)">"</span>)</pre>
</div>
<pre>1. Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误。<br>执行 LPUSH 命令后,列表的长度。</pre>
<div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="复制代码"><img src="//common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></span></div>
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.lpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是1</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是2</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span>,2,3,4) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是5</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.set(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是 True</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2</span><span style="color: rgba(128, 0, 0, 1)">"</span>,2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是 redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value,原因是键 2 是字符串类型,我们用list中的lpush给他添加元素</span></pre>
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="复制代码"><img src="//common.cnblogs.com/images/copycode.gif" alt="复制代码"></a></span></div></div>
<pre>2. Rpush 命令用于将一个或多个值插入到列表的尾部(最右边)。<br>如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。<br>执行 RPUSH 操作后,列表的长度。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是1</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2</span><span style="color: rgba(128, 0, 0, 1)">"</span>,2,3) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是3</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2</span><span style="color: rgba(128, 0, 0, 1)">"</span>,4,5) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是5</span></pre>
</div>
<p>数据格式: </p>
<p> <img src="https://www.icode9.com/i/i/?n=15&i=blog/945786/201706/945786-20170622185306288-1902959097.png" alt=""></p>
<pre>3.Blpop 命令移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。<br>如果列表为空,返回一个 None 。 否则,返回一个含有两个元素的列表,第一个元素是被弹出元素所属的 key ,第二个元素是被弹出元素的值。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">3</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,2,3,4,5,6,) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是6</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.blpop(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">3</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是('3', '1')</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.blpop(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">3</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是('3', '2')</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.blpop(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">3</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是('3', '3')</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.blpop(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">4</span><span style="color: rgba(128, 0, 0, 1)">"</span>,timeout=2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">因为键 4 不存在,所以2秒后输出的结果是None</span></pre>
</div>
<pre>4.Brpop 命令移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。<br>假如在指定时间内没有任何元素被弹出,则返回一个None 和等待时长。 反之,返回一个含有两个元素的列表,第一个元素是被弹出元素所属的 key ,第二个元素是被弹出元素的值。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">4</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,2,3,4,5,6,) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是6</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.brpop(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">4</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是('4', '6')</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.brpop(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">4</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是('4', '5')</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.brpop(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">4</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是('4', '4')</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.brpop(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">5</span><span style="color: rgba(128, 0, 0, 1)">"</span>,timeout=2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">因为键 4 不存在,所以2秒后输出的结果是None</span></pre>
</div>
<pre>5. Brpoplpush 命令从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。<br>假如在指定时间内没有任何元素被弹出,则返回一个 None 和等待时长。 反之,返回一个含有两个元素的列表,第一个元素是被弹出元素的值,第二个元素是等待时长。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">11</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,2,3) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是3</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">22</span><span style="color: rgba(128, 0, 0, 1)">"</span>,4,5,6,) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是3</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.brpoplpush(src=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">11</span><span style="color: rgba(128, 0, 0, 1)">"</span>,dst=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">22</span><span style="color: rgba(128, 0, 0, 1)">"</span>,timeout=2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是3<br><br></span>print r.brpoplpush(src="44",dst="22",timeout=2) #键44 不存在,输出的结果是None</pre>
</div>
<table style="height: 184px; width: 854px" border="0">
<tbody>
<tr>
<td>
<p>键 11 的值: </p>
<p><img src="https://www.icode9.com/i/i/?n=15&i=blog/945786/201706/945786-20170622191918288-1626530719.png" alt=""></p>
<p> </p>
</td>
<td>
<p>键22 的值:</p>
<p><img src="https://www.icode9.com/i/i/?n=15&i=blog/945786/201706/945786-20170622192157648-338587223.png" alt=""></p>
<p> </p>
</td>
</tr>
</tbody>
</table>
<p> </p>
<p>6. Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。</p>
<pre><em>列表中下标为指定索引值的元素。 如果指定索引值不在列表的区间范围内,返回 None 。<br></em></pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">6</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,2,3) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是3</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lindex(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">6</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是2</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lindex(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">6</span><span style="color: rgba(128, 0, 0, 1)">"</span>,2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是3</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lindex(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">6</span><span style="color: rgba(128, 0, 0, 1)">"</span>,3) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是None</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lindex(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">6</span><span style="color: rgba(128, 0, 0, 1)">"</span>,4) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是None</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lindex(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">6</span><span style="color: rgba(128, 0, 0, 1)">"</span>,-1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是3</span></pre>
</div>
<p>7. Linsert 命令用于在列表的元素前或者后插入元素。</p>
<pre><em>当指定元素不存在于列表中时,不执行任何操作。 当列表不存在时,被视为空列表,不执行任何操作。 如果 key 不是列表类型,返回一个错误。<br>如果命令执行成功,返回插入操作完成之后,列表的长度。 如果没有找到指定元素 ,返回 -1 。 如果 key 不存在或为空列表,返回 0 。<br></em></pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">7</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是1</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">7</span><span style="color: rgba(128, 0, 0, 1)">"</span>,2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是2</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.linsert(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">7</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">BEFORE</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2</span><span style="color: rgba(128, 0, 0, 1)">"</span>,12) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是2</span></pre>
</div>
<p>插入后的结果是:<img src="https://www.icode9.com/i/i/?n=15&i=blog/945786/201706/945786-20170622195016163-2133140808.png" alt=""></p>
<pre> 8. Llen 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.llen(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">8</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">key 不存在,输出的结果是0</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">8</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是1</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.llen(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">8</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是1</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">8</span><span style="color: rgba(128, 0, 0, 1)">"</span>,2,3) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是3</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.llen(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">8</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是3</span></pre>
</div>
<pre>9. Lpop 命令用于移除并返回列表的第一个元素。<br>列表的第一个元素。 当列表 key 不存在时,返回 None 。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.lpop(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">9</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是None</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">9</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是1</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">9</span><span style="color: rgba(128, 0, 0, 1)">"</span>,2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是2</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lpop(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">9</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是1</span></pre>
</div>
<p>说明:被移除掉的是第一个值 1, 值2没有被移除</p>
<pre>10.Lpushx 将一个或多个值插入到已存在的列表头部,列表不存在时操作无效。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">10</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是1</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpushx(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">10</span><span style="color: rgba(128, 0, 0, 1)">"</span>,2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是2</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpushx(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">10</span><span style="color: rgba(128, 0, 0, 1)">"</span>,3) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是3</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpushx(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">11</span><span style="color: rgba(128, 0, 0, 1)">"</span>,3) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是5</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lrange(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">10</span><span style="color: rgba(128, 0, 0, 1)">"</span>,0,-1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是['1', '2', '3']</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lrange(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">11</span><span style="color: rgba(128, 0, 0, 1)">"</span>,0,-1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是['1', '2', '1', '2', '3']</span></pre>
</div>
<pre>11.Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素,以此类推。<br> 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">11</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是2</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">11</span><span style="color: rgba(128, 0, 0, 1)">"</span>,3,4) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是4</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lrange(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">11</span><span style="color: rgba(128, 0, 0, 1)">"</span>,0,-1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是['1', '2', '3', '4']</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lrange(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">11</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">输出的结果是['2', '3']</span></pre>
</div>
<pre>12.Lrem 根据参数 COUNT 的值,移除列表中与参数 VALUE 相等的元素。<br>COUNT 的值可以是以下几种:<br>count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。<br>count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。<br>count = 0 : 移除表中所有与 VALUE 相等的值。<br>被移除元素的数量。 列表不存在时返回 0 。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">12</span><span style="color: rgba(128, 0, 0, 1)">"</span>, 1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是1</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">12</span><span style="color: rgba(128, 0, 0, 1)">"</span>, 1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是2</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">12</span><span style="color: rgba(128, 0, 0, 1)">"</span>, 2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是3</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">12</span><span style="color: rgba(128, 0, 0, 1)">"</span>, 1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是4</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lrem(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">12</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,-2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是2</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lrange(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">12</span><span style="color: rgba(128, 0, 0, 1)">"</span>,0,-1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是['1', '2']</span></pre>
</div>
<pre>13. Lset 通过索引来设置元素的值。<br>当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误<br>操作成功返回 True ,否则返回错误信息。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">13</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,2,3,4) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是4</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lset(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">13</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,5) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是True</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lrange(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">13</span><span style="color: rgba(128, 0, 0, 1)">"</span>,0,-1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是['1', '5', '3', '4']</span></pre>
</div>
<pre>14. Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。<br>下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 <br>你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">14</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,2,3,4) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是4</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.ltrim(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">14</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,-2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是True</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lrange(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">14</span><span style="color: rgba(128, 0, 0, 1)">"</span>,0,-1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是['2', '3']</span></pre>
</div>
<div class="cnblogs_code">
<pre>15<span style="color: rgba(0, 0, 0, 1)">. Rpop 命令用于移除并返回列表的最后一个元素。
列表的最后一个元素。 当列表不存在时,返回 None 。</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">15</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,2,3,4) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是4</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpop(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">15</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是4</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lrange(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">15</span><span style="color: rgba(128, 0, 0, 1)">"</span>,0,-1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是['1', '2', '3']</span></pre>
</div>
<pre>16.Rpoplpush 命令用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">16</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,2,3,4) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是4</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">17</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1,2,3,4) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是4</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpoplpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">16</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">17</span><span style="color: rgba(128, 0, 0, 1)">"</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是4</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lrange(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">16</span><span style="color: rgba(128, 0, 0, 1)">"</span>,0,-1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是['1', '2', '3']</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lrange(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">17</span><span style="color: rgba(128, 0, 0, 1)">"</span>,0,-1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是['4', '1', '2', '3', '4']</span></pre>
</div>
<pre>17.Rpushx 命令用于将一个或多个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效。</pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span> r.rpushx(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">18</span><span style="color: rgba(128, 0, 0, 1)">"</span>,1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 因为键18 不存在,所以插入失败,输出的结果是0</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpush(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">18</span><span style="color: rgba(128, 0, 0, 1)">"</span>,2) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是1</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.rpushx(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">18</span><span style="color: rgba(128, 0, 0, 1)">"</span>,3) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是2</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> r.lrange(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">18</span><span style="color: rgba(128, 0, 0, 1)">"</span>,0,-1) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输出的结果是['2', '3']</span></pre>
</div>
<pre><strong> </strong></pre>
<p>redis安装和配置(一) http://www.cnblogs.com/xuchunlin/p/6676308.html</p>
<p>redis学习 (key)键,Python操作redis 键 (二) http://www.cnblogs.com/xuchunlin/p/7061524.html</p>
<p>Python操作redis字符串(String)详解 (三) http://www.cnblogs.com/xuchunlin/p/7062065.html</p>
<p>Python操作redis系列以 哈希(Hash)命令详解(四) http://www.cnblogs.com/xuchunlin/p/7064860.html</p>
<p>Python操作redis系列之 列表(list) (五) http://www.cnblogs.com/xuchunlin/p/7067154.html</p>
<p>Python操作redis学习系列之(集合)set,redis set详解 (六)http://www.cnblogs.com/xuchunlin/p/7070267.html</p>
<p>python 操作redis之——有序集合(sorted set) (七) http://www.cnblogs.com/xuchunlin/p/7097272.html</p>
<p>python 操作redis之——HyperLogLog (八) http://www.cnblogs.com/xuchunlin/p/7097272.html</p>
<p>redis.windows.conf各项配置参数介绍 (九) http://www.cnblogs.com/xuchunlin/p/7097729.html</p>
<pre><strong><br><br><br>附录:redis命令</strong></pre>
<table class="table table-bordered table-striped" style="height: 419px; width: 1077px">
<tbody>
<tr><th class="eye-protector-processed">命令</th><th class="eye-protector-processed">描述</th></tr>
<tr>
<td><a href="http://www.redis.net.cn/order/3577.html">Redis Blpop 命令</a></td>
<td>移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</td>
</tr>
<tr>
<td class="eye-protector-processed"><a href="http://www.redis.net.cn/order/3578.html">Redis Brpop 命令</a></td>
<td class="eye-protector-processed">移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</td>
</tr>
<tr>
<td><a href="http://www.redis.net.cn/order/3579.html">Redis Brpoplpush 命令</a></td>
<td>从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。</td>
</tr>
<tr>
<td class="eye-protector-processed"><a href="http://www.redis.net.cn/order/3580.html">Redis Lindex 命令</a></td>
<td class="eye-protector-processed">通过索引获取列表中的元素</td>
</tr>
<tr>
<td><a href="http://www.redis.net.cn/order/3581.html">Redis Linsert 命令</a></td>
<td>在列表的元素前或者后插入元素</td>
</tr>
<tr>
<td class="eye-protector-processed"><a href="http://www.redis.net.cn/order/3582.html">Redis Llen 命令</a></td>
<td class="eye-protector-processed">获取列表长度</td>
</tr>
<tr>
<td><a href="http://www.redis.net.cn/order/3583.html">Redis Lpop 命令</a></td>
<td>移出并获取列表的第一个元素</td>
</tr>
<tr>
<td class="eye-protector-processed"><a href="http://www.redis.net.cn/order/3584.html">Redis Lpush 命令</a></td>
<td class="eye-protector-processed">将一个或多个值插入到列表头部</td>
</tr>
<tr>
<td><a href="http://www.redis.net.cn/order/3585.html">Redis Lpushx 命令</a></td>
<td>将一个或多个值插入到已存在的列表头部</td>
</tr>
<tr>
<td class="eye-protector-processed"><a href="http://www.redis.net.cn/order/3586.html">Redis Lrange 命令</a></td>
<td class="eye-protector-processed">获取列表指定范围内的元素</td>
</tr>
<tr>
<td><a href="http://www.redis.net.cn/order/3587.html">Redis Lrem 命令</a></td>
<td>移除列表元素</td>
</tr>
<tr>
<td class="eye-protector-processed"><a href="http://www.redis.net.cn/order/3588.html">Redis Lset 命令</a></td>
<td class="eye-protector-processed">通过索引设置列表元素的值</td>
</tr>
<tr>
<td><a href="http://www.redis.net.cn/order/3589.html">Redis Ltrim 命令</a></td>
<td>对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。</td>
</tr>
<tr>
<td class="eye-protector-processed"><a href="http://www.redis.net.cn/order/3590.html">Redis Rpop 命令</a></td>
<td class="eye-protector-processed">移除并获取列表最后一个元素</td>
</tr>
<tr>
<td><a href="http://www.redis.net.cn/order/3591.html">Redis Rpoplpush 命令</a></td>
<td>移除列表的最后一个元素,并将该元素添加到另一个列表并返回</td>
</tr>
<tr>
<td class="eye-protector-processed"><a href="http://www.redis.net.cn/order/3592.html">Redis Rpush 命令</a></td>
<td class="eye-protector-processed">在列表中添加一个或多个值</td>
</tr>
<tr>
<td><a href="http://www.redis.net.cn/order/3593.html">Redis Rpushx 命令</a></td>
<td>为已存在的列表添加值</td>
</tr>
</tbody>
</table>
<pre><br><br></pre>
<pre><br><br></pre>
<pre><br><br></pre>
<pre><br><br></pre>
<pre><em id="__mceDel"> </em></pre>
<pre><em id="__mceDel"> </em></pre>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
</div>
内容总结
以上是互联网集市为您收集整理的Python操作redis系列--列表(list)全部内容,希望文章能够帮你解决Python操作redis系列--列表(list)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。