. Python Redis String Operations list operation set operation

Source: Internet
Author: User
Tags set set

1. Redis string Manipulation

String manipulation

Reids currently offers 5 data types: String type, list type, set set type, ordered collection type, hash type, and the following is a detailed description of their use.

A string in Redis is stored in memory by a key corresponding to a value.

Such as:

R.set ("name", "Ling")

How to use Set:

Set (name, value, Ex=none, Px=none, Nx=false, Xx=false)

EX, expiry time (seconds)

PX, Expiration Time (ms)

NX, if set to true, the current set operation executes only if name does not exist, with SETNX (name, value)

XX, if set to true, the current set operation is performed only if name exists

Get (name) gets the value of name

Print (R.get ("name"))

Mset () batch set multiple values

R.mset (name1= "Shang", name2= "Ling")

R.mset ({"Name3": "Kong", "name4": "gu"})

Mget (keys, *args) bulk fetch values

Print (R.mget ("name1", "name2", "Name3", "Name4"))

Append (name, value)

R.append ("name", "Lisi")

Print (R.get ("name"))



2. List

The list in Redis is stored in memory according to a list of name

Lpush (name,values)

# add elements to the list in name, and each new element is added to the leftmost

R.lpush ("List_name", 2)

R.lpush ("List_name", 3,4,5) #保存在列表中的顺序为5, 4,3,2

Rpush (name,values)

#同lpush, but each new element is added to the far right of the list

Lpushx (Name,value)

#在name对应的list中添加元素, the value is added to the leftmost side of the list only if name already exists

Rpushx (Name,value)

#在name对应的list中添加元素, the value is added to the far right of the list only if name already exists

Llen (name)

# name corresponds to the number of list elements

Print (R.llen ("List_name")),

Linsert (Name,where,refvalue,value): Inserts a new value before and after a value in the list corresponding to name

#参数:

# Name,redis's name

# Where,before or after

# Refvalue, the benchmark value, that is: Insert data before and after it

# value, the data to be inserted

R.lset (Name,index,value): Re-assigns a value to one of the index positions in the list corresponding to name.

Parameters

# Name,redis's name

# index position of Index,list

# value, values to set

R.lrem (name,value,num): Deletes the specified value in the list corresponding to name

Parameters

# Name,redis's name

# value, values to remove

# num, num=0, removes all specified values from the list;

# num=2, from front to back, delete 2;

# num=-2, from back forward, delete 2

Lpop (name): Gets the first element on the left side of the list corresponding to name and removes it from the list, and the return value removes the value of that element

#扩展: Rpop (name) indicates right-to-left operation

Lindex (Name,index): The list element in the list of name corresponding to the index

Lrange (name,start,end): The list shard in name corresponds to get data


#!/usr/bin/env python# -*- coding:utf-8 -*-import redispool = redis. ConnectionPool (host= "192.168.48.131",  port=6379, db=0) R = redis. Redis (Connection_pool=pool) # lpush   adds an element to the left of list             left# rpush   add an element to the right of the list             rightr.lpush ("List1",  "Test1") R.rpush ("List1",  "Ling") R.lpush ("List1",  "Test2") R.lpush ("List1",  "Test3") R.lpush ("List1",  "Test2") R.lpush ("List1",  2, 3, 4) R.lpush ("List1",  "Test2") Print (R.lrange ("List1",  0, -1)) #  The final list result is      [4, 3, 2,  "Test3",  "test2",  "Test1",  "Ling"]#  add an element in the middle position #  Linsert (Name,where,refvalue,value) # name    represents the key value for the list # where      after   before# refvalue&nbspAn element in the    list # value       the value you want to add R.linsert ("List1",  "After",   "test2",  "Hello") Print (R.lrange ("List1",  0, -1)) R.lset ("List1",  2,   "World") # lset   change the value of an element # r.lset (name,index,value) print (R.lrange ("List1",  0,  -1)) Print (R.lindex ("List1",  2)) # lindex    View the list value print (R.lpop ("List1")) #  lpop deletes an element from the leftmost list, returns the value of the deleted element print (R.lrange ("List1",  0, -1)) R.lrem ("List1",  "World") #  r.lrem (name,value,num): # num,  num=0, delete all the specified values in the list; # num=2, from the front to the back, delete 2; # num=-2, Remove 2 print (R.lrange ("List1",  0, -1) from rear forward
Results: [' test2 ', ' 4 ', ' 3 ', ' 2 ', ' test2 ', ' test3 ', ' test2 ', ' test1 ', ' ling ' [' test2 ', ' Hello ', ' 4 ', ' 3 ', ' 2 ', ' test2 ', ' test3 ', ' Test2 ', ' test1 ', ' ling ' [' test2 ', ' Hello ', ' World ', ' 3 ', ' 2 ', ' test2 ', ' test3 ', ' test2 ', ' test1 ', ' ling ']worldtest2[' hel  Lo ', ' World ', ' 3 ', ' 2 ', ' test2 ', ' test3 ', ' test2 ', ' test1 ', ' ling ' [' Hello ', ' 3 ', ' 2 ', ' test2 ', ' test3 ', ' test2 ', ' test1 ', ' Ling ']



3. Set operation

Set set is a list that is not duplicated

Sadd (name,values)

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

R.sadd ("Set_name", "AA")

R.sadd ("Set_name", "AA", "BB")

Smembers (name)

#获取name对应的集合的所有成员

SCard (name)

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

R.scard ("Set_name")

Sinter (keys, *args)

# Get the set of multiple name corresponding sets

R.sadd ("Set_name", "AA", "BB")

R.sadd ("set_name1", "BB", "CC")

R.sadd ("set_name2", "BB", "CC", "DD")

Print (R.sinter ("Set_name", "set_name1", "set_name2"))

#输出: {BB}

Sismember (name, value)

#检查value是否是name对应的集合内的元素

Spop (name)

#从集合的右侧移除一个元素, and return it

Sunion (keys, *args)

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

R.sunion ("Set_name", "set_name1", "set_name2")

Srem (name, value) removes an element from the collection

R.srem ("Set_name", "AA")


. Python Redis String Operations list operation set operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.