python--Redis Set

Source: Internet
Author: User
Tags set set

I. Unordered collection

Set operation, set set is a list that does not allow duplicates

  1.1 Sadd (name, values)

# add elements to the collection that corresponds to name

  1.2 Smembers (name)

# get the name corresponding to all members of the collection R.sadd (' s1 ', ' t1 ', ' T2 ', ' T3 ', ' T1 ') print (r.smembers (' s1 ')) # output {b ' t1 ', b ' T2 ', b ' T3 '}# set is go heavy

  1.3 SCard (name)

#获取name对应的集合中元素个数print (R.scard (' s1 ')) #输出3

  1.4 Sdiff (keys, *args)

  1.5 Sdiffstore (dest, Keys, *args)

# gets the first name corresponding to the collection and is not in the other name corresponding to the collection, #再将其新加入到dest对应的集合中print (' S1: ', r.smembers (' s1 ')) print (' S2: ', r.smembers (' s2 ')) R.sdiffstore (' S3 ', ' s1 ', ' s2 ') print (' S3: ', R.smembers (' S3 ')) #输出s1: {b ' t1 ', b ' T3 ', b ' T2 '}s2: {b ' T4 ', B ' T5 ', b ' T1 '}s3: {b ' T3 ', B ' T2 '}

  1.6 Sinter (keys, *args)

# get the intersection of multiple collections print (' S1: ', r.smembers (' s1 ')) print (' S2: ', r.smembers (' s2 ')) print (' Intersection: ', R.sinter (' s1 ', ' s2 ') #输出s1: {b ' T2 ', b ' T1 ', b ' T3 '}s2: {b ' t1 ', b ' T4 ', B ' T5 '} intersection: {b ' t1 '}

  1.7 Sinterstore (dest, Keys, *args)

# get more than one name corresponding to the set of sets, and then add it to the dest corresponding set of print (' S1: ', r.smembers (' s1 ')) print (' S2: ', r.smembers (' s2 ')) R.sinterstore (' S4 ') , ' s1 ', ' s2 ') print (' S4: ', R.smembers (' S4 ')) #输出s1: {b ' t3 ', b ' T2 ', b ' T1 '}s2: {b ' t1 ', B ' T5 ', b ' T4 '}s4: {b ' t1 '}

  1.8 Sismember (name, value)

# Check if value is a member of the collection of name corresponding to print (R.sismember (' s1 ', ' t1 ')) print (R.sismember (' s1 ', ' T5 ')) #输出TrueFalse

  1.9 Smove (SRC, DST, value)

# move a member from one collection to another set print (' S1: ', r.smembers (' s1 ')) print (' S2: ', r.smembers (' s2 ')) r.smove (' s1 ', ' s2 ', ' T2 ') print (' S1: ', r.smembers (' s1 ') print (' S2: ', r.smembers (' s2 ')) # output S1: {b ' t2 ', b ' T1 ', b ' T3 '}s2: {b ' T4 ', b ' T1 ', B ' T5 '}s1: {b ' t1 ', B ' T3 '}s2: {b ' T4 ', b ' T2 ', b ' T1 ', B ' T5 '}

  1.10 Spop (name)

# Remove a member from the right (tail) of the collection and return it to print (' S1: ', r.smembers (' s1 ') r.spop (' s1 ') print (' S1: ', r.smembers (' s1 ')) #输出s1: {b ' t1 ', B ' T3 '}S1: {b ' t1 '}

  1.11 Srandmember (name, numbers)

# randomly gets the numbers element print (' s2: ', r.smembers (' s2 ')) Print (R.srandmember (' S2 ', 3)) from the collection of name corresponding to a random number of 3 numbers s2 from s2: {b ' T5 ', B ' #输出 T2 ', b ' T1 ', b ' T4 '}[b ' T5 ', b ' T2 ', b ' T1 ']

  1.12 Srem (name, values)

# Delete certain values in name corresponding set print (' S2: ', r.smembers (' s2 ')) r.srem (' S2 ', ' T5 ') print (' S2: ', r.smembers (' s2 ')) #输出s2: {b ' t2 ', b ' T1 ' , B ' T5 ', b ' T4 '}s2: {b ' t2 ', b ' T1 ', b ' T4 '}

  1.13 sunion (keys, *args)

# get more than one name corresponding to the set of Set print (R.smembers (' S3 ')) Print (R.smembers (' S4 ')) Print (R.sunion (' S3 ', ' S4 ')) #输出 {b ' t3 ', b ' T2 '}{b ' T1 '}{b ' t1 ', b ' T3 ', b ' T2 '}

  1.14 Sunionstore (Dest,keys, *args)

# get the set of one more name corresponding to the collection and save the result to the dest corresponding set of print (' S3: ', R.smembers (' S3 ')) print (' S4: ', R.smembers (' S4 ')) R.sunionstore (' S6 ', ' S3 ', ' S4 ') print (' S6: ', R.smembers (' S6 ')) #输出s3: {b ' t2 ', b ' T3 '}S4: {b ' t1 '}s6: {b ' t2 ', b ' T1 ', b ' T3 '}

  1.15 Sscan (name, cursor=0, Match=none, Count=none)

# Shard gets data print (' Test_info: ', r.smembers (' Test_info ')) print (R.sscan (' Test_info ', 0, match= ' j* ')) # output test_info: {b ' Jerry ', B ' Jack ', B ' Tom ', B ' Sam '} (0, [b ' Jack ', B ' Jerry '))

  1.16 Sscan_iter (name, Match=none, Count=none)

# Operations with strings, used for incremental iterations to get elements in batches, avoiding too much memory consumption

  

Ii. ordered set

An ordered set, on the basis of the set, sorts each element; The ordering of the elements needs to be compared according to another value, so for an ordered set, each element has two values, namely: values and fractions, which are specifically used for sorting.

  2.1 Zadd (name, *args, **kwargs)

# add elements to the ordered collection of name: #     Zadd (' zz ', ' N1 ', 1, ' N2 ', 2)     # or     # zadd (' zz ', n1=11, n2=22) r.zadd (' Z1 ', ' T1 ', ten, ' t 2 ', 5, ' T3 ', 4, ' T4 ', 8)

  2.2 Zrange (name, start, end, Desc=false, Withscores=false, Score_cast_func=float)

# Gets the element of the ordered collection of name corresponding to the index range # parameter:    # Name,redis name    # start, ordered set index start position (non-fractional)    # end, ordered collection index end position (non-fractional)    # desc , collation, default by fractions from small to large    # withscores, whether to get the fraction of the element, the default only gets the value of the element    # Score_cast_func, the function of the data conversion to the fraction # more:    # from big to small sort    # Zrevrange (name, start, end, Withscores=false, score_cast_func=float)     # Gets the element of the ordered collection of name corresponding to the range of fractions    # Zrangebyscore (name, Min, Max, Start=none, Num=none, Withscores=false, score_cast_func=float)    # Sort from big to small    # Zrevrangebyscore (name, max, Min, Start=none, Num=none, Withscores=false, score_cast_func=float) print (R.zrange (' Z1 ', 0 ,-1)) Print (R.zrange (' Z1 ', 0,-1, withscores=true)) #输出 [b ' T3 ', b ' T2 ', b ' T4 ', b ' T1 ' [(b ' T3 ', 4.0), (b ' T2 ', 5.0), (b ' T4 ', 8.0 ), (b ' T1 ', 10.0)]

  2.3 Zcard (name)

# Gets the number of ordered collection elements that name corresponds to print (R.zcard (' Z1 ')) #输出4

  2.4 Zcount (name, Min, max)

# Gets the number of points in the ordered collection of name corresponding to [Min,max] Print (R.zrange (' Z1 ', 0, 1, withscores=true)) print (R.zcount (' Z1 ', 5, 8)) #输出2

  2.5 Zincrby (name, value, amount)

# name corresponds to the ordinal collection of the value in the score increment amountprint (R.zrange (' Z1 ', 0,-1, withscores=true)) print (R.zincrby (' Z1 ', ' T3 ', 6)) print ( R.zrange (' Z1 ', 0,-1, withscores=true)) #输出 [(b ' T3 ', 4.0), (b ' T2 ', 5.0), (b ' T4 ', 8.0), (b ' T1 ', 10.0)]10.0[(b ' T2 ', 5.0), (b ' t 4 ', 8.0), (b ' T1 ', 10.0), (b ' T3 ', 10.0)]

  2.6 Zrank (name, value)

# Gets the rank of a value in an ordered set of name (starting from 0) # more:    # Zrevrank (name, value), from large to small sort print (R.zrange (' Z1 ', 0,-1, withscores=true)) PRI NT (R.zrank (' Z1 ', ' t1 ')) print (R.zrevrank (' Z1 ', ' T1 ')) #输出 [(b ' T2 ', 5.0), (b ' T4 ', 8.0), (b ' T1 ', 10.0), (b ' T3 ', 10.0)]21

 2.7 Zrem (name, values)

# delete name corresponding to the ordered collection of values is a member of values # such as: Zrem (' zz ', [' s1 ', ' s2 ']) print (R.zrange (' Z1 ', 0,-1, Withscores=true)) R.zrem (' Z1 ', ' T1 ') Print (R.zrange (' Z1 ', 0,-1, withscores=true)) #输出 [(b ' T2 ', 5.0), (b ' T4 ', 8.0), (b ' T1 ', 10.0), (b ' T3 ', 10.0)] [(b ' T2 ', 5.0), ( B ' T4 ', 8.0), (b ' T3 ', 10.0)

  2.8 Zremrangebyrank (name, Min, max)

# delete R.zremrangebyrank (' Z1 ', 1, 6) print (R.zrange (' Z1 ', 0,-1, withscores=true)) #输出 [(b ' T2 ', 5.0)] According to the range of lines removed

 2.9 Zremrangebyscore (name, Min, max)

# Delete Print (R.zrange (' Z1 ', 0,-1, withscores=true)) from the score range R.zremrangebyscore (' Z1 ', 1, 6) print (R.zrange (' Z1 ', 0,-1, withscores=true)) #输出 (b ' 5.0 ', T4), (b ' 8.0 ', 4.0), (b ' T2 ', T1), (b ' 10.0 ', T4)] [(b ', ', ', ')]

  2.10 Zscore (name, value)

# gets the name corresponding to the score of value in the Ordered collection print (R.zrange (' Z1 ', 0,-1, withscores=true)) print (R.zscore (' Z1 ', ' T1 ')) #输出 [(b ' T4 ', 8.0), (b ' T1 ', 10.0)]10.0

  2.11 Zinterstore (dest, Keys, Aggregate=none)

# gets the intersection of two ordered sets, and if the same value is encountered, the value of the following aggregate is: SUM MIN MAX default aggregate ( ' Z2: ', R.zrange (' Z2 ', 0,-1, withscores=true)) print (' Z3: ', R.zrange (' Z3 ', 0,-1, Withscores=true)) R.zinterstore (' Z6 ', {  ' Z2 ', ' Z3 '}) print (' Z6: ', R.zrange (' Z6 ', 0,-1, withscores=true)) # Output Z2: [(b ' T3 ', 4.0 '), (b ' T2 ', 5.0), (b ' T4 ', 8.0), (b ' T1 ', 10.0)]z3: [(b ' T3 ', 2.0), (b ' T1 ', 6.0), (b ' T2 ', 7.0), (b ' T4 ', 12.0)]z6: [(b ' T3 ', 6.0), (b ' T2 ', 12.0), (b ' T1 ', 16.0), (b ' T4 ', 20.0)] 
Print (' Z2: ', R.zrange (' Z2 ', 0,-1, withscores=true)) print (' Z3: ', R.zrange (' Z3 ', 0,-1, Withscores=true)) R.zinterstore ( ' Z7 ', {' Z2 ', ' Z3 '}, aggregate= ' MIN ') print (' Z7: ', R.zrange (' Z7 ', 0,-1, Withscores=true)) R.zinterstore (' Z8 ', {' Z2 ', ' Z3 ') }, aggregate= ' MAX ') print (' Z8: ', R.zrange (' Z8 ', 0,-1, withscores=true)) # Output Z2: [(b ' T3 ', 4.0), (b ' T2 ', 5.0), (b ' T4 ', 8.0), (b ' T1 ', 10.0)] Z3: [(b ' T3 ', 2.0), (b ' T1 ', 6.0), (b ' T2 ', 7.0), (b ' T4 ', 12.0)]z7: [(b ' T3 ', 2.0), (b ' T2 ', 5.0), (b ' T1 ', 6.0), (b ' T4 ', 8.0)]z 8: [(b ' T3 ', 4.0), (b ' T2 ', 7.0), (b ' T1 ', 10.0), (b ' T4 ', 12.0)]

If the number of two of these sets does not match, then the individual value will not be calculated

  2.12 Zunionstore (dest, Keys, Aggregate=none)

Print (' Z2: ', R.zrange (' Z2 ', 0,-1, withscores=true)) print (' Z3: ', R.zrange (' Z3 ', 0,-1, Withscores=true)) R.zunionstore ( ' Z10 ', {' Z2 ', ' Z3 '}) print (' Z10: ', R.zrange (' Z10 ', 0,-1, Withscores=true)) #输出z2: [(b ' T3 ', 4.0), (b ' T2 ', 5.0), (b ' T4 ', 8.0) ]Z3: [(b ' T3 ', 2.0), (b ' T1 ', 6.0), (b ' T2 ', 7.0), (b ' T4 ', 12.0)]z10: [(b ' T1 ', 6.0), (b ' T3 ', 6.0), (b ' T2 ', 12.0), (b ' T4 ', 20.) 0)]

2.13 Zscan (name, cursor=0, Match=none, Count=none, Score_cast_func=float)

2.14 Zscan_iter (name, Match=none, Count=none,score_cast_func=float)

# Similar to string, with a new score_cast_func for a string to manipulate fractions

 

python--Redis Set

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.