Python -- Redis Set, python -- redisset

Source: Internet
Author: User

Python -- Redis Set, python -- redisset
1. unordered set

Set operation. The Set operation is a list that cannot be repeated.

  1.1 sadd (name, values)

# Add elements to the set corresponding to name

  1.2 smembers (name)

# Obtain all members of the set corresponding to name. sadd ('s1 ', 't1', 't2', 't3', 't1') print (r. smembers ('s1') # output {B 't1 ', B 't2', B 't3 '} # The set is deduplicated.

  1.3 scard (name)

# Obtain the number of elements in the set corresponding to name print (r. scard ('s1') # output 3

  1.4 sdiff (keys, * args)

# Print (r. smembers ('s1') print (r. smembers ('s2') print (r. sdiff ('s1', 's2') # output {B 't3 ', B 't1', B 't2'} {B 't1', B 't5 ', B 't4 '} {B' t3 ', B' t2 '} # t2 and t3 in set s1 are not in s2.

  1.5 sdiffstore (dest, keys, * args)

# Obtain the set corresponding to the first name and not the set corresponding to other names. # Add the new one to the set corresponding to dest, print ('s1: ', r. smembers ('s1') print ('s2: ', r. smembers ('s2') r. sdiffstore ('s3', 's1', 's2') print ('s3: ', r. smembers ('s3') # output s1: {B 't1', B 't3', B 't2'} s2: {B 't4', B 't5 ', B 't1'} s3: {B 't3 ', B 't2 '}

  1.6 sinter (keys, * args)

# Obtain the print ('s1: ', r. smembers ('s1') print ('s2: ', r. smembers ('s2 ') print ('intersection:', r. sinter ('s1', 's2') # output s1: {B 't2', B 't1', B 't3 '} s2: {B 't1 ', B 't4', B 't5'} intersection: {B 't1 '}

  1.7 sinterstore (dest, keys, * args)

# Obtain the union of the set corresponding to one more name, and then add it to the set corresponding to dest ('s1: ', r. smembers ('s1') print ('s2: ', r. smembers ('s2') r. sinterstore ('s4 ', 's1', 's2') print ('s4:', r. smembers ('s4 ') # output s1: {B 't3', B 't2', B 't1'} s2: {B 't1', B 't5 ', B 't4 '} s4: {B 't1 '}

  1.8 sismember (name, value)

# Check whether value is a member of the set corresponding to name print (r. sismember ('s1', 't1') print (r. sismember ('s1 ', 't5') # output TrueFalse

  1.9 smove (src, dst, value)

# Move a Member from a 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 Set and return print ('s1: ', r. smembers ('s1') r. spop ('s1') print ('s1: ', r. smembers ('s1') # output s1: {B 't1 ', B 't3'} s1: {B 't1 '}

  1.11 srandmember (name, numbers)

# Obtain the numbers element print ('s2: ', r. smembers ('s2') print (r. srandmember ('s2 ', 3) # output: Randomly obtain three numbers from s2: {B 't5', B 't2 ', B 't1 ', B 't4 '} [B' t5 ', B' t2 ', B' t1 ']

  1.12 srem (name, values)

# Delete some print ('s2: ', r. smembers ('s2') r. srem ('s2 ', 't5') print ('s2: ', r. smembers ('s2 ') # output s2: {B 't2', B 't1 ', B 't5', B 't4 '} s2: {B 't2 ', B 't1', B 't4 '}

  1.13 sunion (keys, * args)

# Obtain the Union print (r. smembers ('s3') print (r. smembers ('s4 ') print (r. sunion ('s3', 's4 ') # output {B 't3', B 't2'} {B 't1'} {B 't1 ', B 't3', B 't2 '}

  1.14 sunionstore (dest, keys, * args)

# Obtain the union of the set corresponding to one more name, and save the result to the print ('s3: ', r. smembers ('s3') print ('s4: ', r. smembers ('s4 ') r. sunionstore ('s6 ', 's3', 's4') print ('s6:', r. smembers ('s6 ') # output 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)

# 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 the same string are used to obtain elements in batches through incremental iteration to avoid high memory consumption

  

Ii. Ordered collection

An ordered set sorts each element based on the set. The sorting of elements needs to be compared based on another value. Therefore, for an ordered set, each element has two values: values and scores, which are used for sorting.

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

# Add elements to the sorted set corresponding to name # For example: # zadd ('zz ', 'n1', 1, 'n2 ', 2) # Or # zadd ('zz ', n1 = 11, n2 = 22) r. zadd ('z1 ', 't1', 10, 't2', 5, 't3', 4, 't4 ', 8)

  2.2 zrange (name, start, end, desc = False, withscores = False, score_cast_func = float)

# Obtain the elements of an ordered set corresponding to name according to the index range # parameter: # name, redis name # start, index start position of an ordered set (non-score) # end, sorted SET index end position (non-score) # desc, sorting rule, sorted by scores from small to large by default # withscores, whether to get the score of the element. By default, only the value of the element is obtained # score_cast_func, function for data conversion of scores # More: # Sort scores from large to small # zrevrange (name, start, end, withscores = False, score_cast_func = float) # obtain the elements of an ordered set corresponding to name according to the score range # 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) # output [B 't3', B 't2', B 't4 ', B 't1'] [(B' t3', 4.0), (B 't2', 5.0), (B' t1', 8.0), (B 't1 ', (10.0)]

  2.3 zcard (name)

# Obtain the number of elements in an ordered set corresponding to name print (r. zcard ('z1') # output 4

  2.4 zcount (name, min, max)

# Obtain the number of scores in the sorted set corresponding to name between [min, max] print (r. zrange ('z1', 0,-1, withscores = True) print (r. zcount ('z1', 5, 8) # output 2

  2.5 zincrby (name, value, amount)

# Amountprint (r. zrange ('z1', 0,-1, withscores = True) print (r. zincrby ('z1 ', 't3', 6) print (r. zrange ('z1', 0,-1, withscores = True) # output [(B 't3 ', 4.0), (B 't2, 5.0 ), (B 't4 ', 8.0), (B 't1', 10.0)] 10.0 [(B 't2', 5.0), (B 't4', 8.0 ), (B 't1 ', 10.0), (B 't3', 10.0)]

  2.6 zrank (name, value)

# Obtain the ranking of a value in an ordered set corresponding to name (starting from 0) # More: # zrevrank (name, value), and print (r. zrange ('z1', 0,-1, withscores = True) print (r. zrank ('z1 ', 'T1') print (r. zrevrank ('z1 ', 't1') # output [(B 't2', 5.0), (B 't4', 8.0), (B 't1 ', 10.0), (B 't3 ', 10.0)] 21

 2.7 zrem (name, values)

# Deleting the value of an ordered set corresponding to name is a member of values # For example, zrem ('zz ', ['s1', 's2']) print (r. zrange ('z1', 0,-1, withscores = True) r. zrem ('z1', 't1') print (r. zrange ('z1', 0,-1, withscores = True) # output [(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 data based on the ranking range. r. zremrangebyrank ('z1', 1, 6) print (r. zrange ('z1', 0,-1, withscores = True) # output [(B 't2', 5.0)]

 2.9 zremrangebyscore (name, min, max)

# Delete print (r. zrange ('z1', 0,-1, withscores = True) r. zremrangebyscore ('z1', 1, 6) print (r. zrange ('z1', 0,-1, withscores = True) # output [(B 't3 ', 4.0), (B 't2, 5.0 ), (B 't4 ', 8.0), (B 't1', 10.0)] [(B 't4', 8.0), (B 't1', 10.0)]

  2.10 zscore (name, value)

# Obtain the score print (r. zrange ('z1', 0,-1, withscores = True) print (r. zscore ('z1 ', 't1') # output [(B 't4', 8.0), (B 't1 ', 10.0)] 10.0

  2.11 zinterstore (dest, keys, aggregate = None)

# Obtain the intersection of two ordered sets. If the same value is encountered, perform the operation according to aggregate # The value of aggregate is sum min max, which defaults to SUMprint ('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)] z8: [(B 't3', 4.0), (B 't2', 7.0 ), (B 't1', 10.0), (B 't4 ', 12.0)]

If the number of the Two sets does not match, the value is not calculated separately.

  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) # output 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 the string, score_cast_func is added to the string to operate the score.

 

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.