Day25--nosql string manipulation, list operation, set operation

Source: Internet
Author: User

Tag: is the middle Number Union Div range operation get string

Manipulation of strings
#redis中的string is stored in memory by a key corresponding to a valus.

1 ImportRedis2 3Pool = Redis. ConnectionPool (host="192.168.1.5", port=6379, db=0)4R = Redis. Redis (connection_pool=pool)5R.set ('name','WXP')6 #print (R.keys ())7 Print(R.get ("name"))8R.mset (name1 ='W', name2 ='x')9 Print(R.mget ('name1','name2'))TenR.mset ({"a":"1","b":"2"}) One Print(R.mget ('a','b'))

Output Result:

Wxp
[' W ', ' X ']
[' 1 ', ' 2 ']

List operations:
Pool = Redis. ConnectionPool (host= "192.168.1.5", port=6379, Db=0)
R = Redis. Redis (Connection_pool=pool)
#lpush add an element to the left of the list
#rpush add an element to the right of the list.
R.lpush (' List1 ', ' test1 ')
R.lpush (' List1 ', ' test2 ')
R.rpush (' List1 ', ' WXP ')
R.lpush (' List1 ', 2,3,4)
Print (R.lrange ("List1", 0,-1))
#最终的list结果是 [4,3,2, "Test1", WXP]

#在中间位置增加一个元素, Linsert
#where represents the key value of the list pair
# The Pavilion element in the Refvalue list
#value the value you want to increase
R.linsert ("List1", "after", "Test2", "Hello")
Print (R.lrange ("List1", 0,-1))
#lset change an element
R.lset ("List1", 2, "word")
Print (R.lrange ("List1", 0,-1))
#lindex查看list某个下标的值
Print (R.lindex ("List1", 2))
#lpop从list的最左边删除一个元素, returns the value of the deleted element
Print (R.lpop ("List1"))
Print (R.lrange ("List1", 0,-1))
#r. Lrem (name,value,num):
#num, num=0, removes all the specified values from the list:
#num = 2, from front to back, delete 2
#num =-2, remove 2 from the back
R.lrem ("List1", "World")
Print (R.lrange ("List1", 0,-1))

Collection operations:

1 ImportRedis2 3Pool = Redis. ConnectionPool (host="192.168.1.5", port=6379, db=0)4R = Redis. Redis (connection_pool=pool)5 #R.sadd (Name,*args) (element cannot be duplicated) collection adds a name corresponding to a collection6R.sadd ("Set_name",'AA','BB','cc')7 #Smember is to view all the elements of a collection8 Print(R.smembers ("Set_name"))9 #SCard equivalent to list Lem to view the number of elementsTen Print(R.scard ("Set_name")) One #Srem (name,value) deletes an element from the specified collection with value AR.srem ("Set_name","AA") - Print(R.smembers ("Set_name")) -R.sadd ("set_name1","cc","DD","ee") the #Sinter intersection of two sets - Print(R.sinter ("Set_name","set_name1")) - #sunion Two sets of aggregates - Print(R.sunion ("Set_name","set_name1"))

Day25--nosql string manipulation, list operation, set operation

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.