Data type: type of data collection (set)

Aggregate data type (set)

A feature of

1, the disorder, the weight to
2, the element is a string type
3, comprising up to 2 ^ 32-1 elements

Two basic commands

# 1, an increase of one or more elements, automatic de-duplication 
    SADD Key member1 member2
 # 2, see the collection of all elements 
    SMEMBERS Key
 # 3, delete one or more elements, automatically ignored element is not present 
    SREM Key member1 member2
 # 4, elements whether there 
    SISMEMBER member Key
 # . 5, the random collection returns the number of elements specified, the default is a 
    SRANDMEMBER Key [COUNT]
 # . 6, the pop-up member (default 1) random pop 
    SPOP Key [COUNT]
 # . 7, the returned collection number of elements, not through the entire collection, but is stored in among the key 
    SCARD key
 # . 8, the set of mobile elements from the source to the target set 
    SMOVE source destination member 
    Examples: SMOVE myset1 myset2 2 
        to myset1 2 is moved to the inside myset2 (equivalent shear) 
# 9, difference (number1 number2. 1 2. 1 2. 4. 3) 
#Difference-set: number of which there is no any number 
    sdiff key1 key2  
 # 10, the difference between the save set to another set 
    SDIFFSTORE Where do you want key1 key2
 # . 11, the intersection (which has the two) 
    SINTER key1 key2
     # the intersection data replication to the new sET 
    SINTERSTORE Where do you want key1 key2
 # . 11, and set (remove duplicate together) 
    SUNION key1 key2 
    SUNIONSTORE Where do you want key1 key2

python operation set

# 1, added to the set of elements corresponding to the name 
Sadd (name, values) 
r.sadd ( " set_name " , " Tom " ) 
r.sadd ( " set_name " , " Tom " , " Jim " ) 

# 2, acquired name All members of the corresponding set of: collection 
smembers (name) 

# . 3, the element number of the acquired name corresponding set 
SCard (name) 
r.scard ( " set_name " ) 

# . 4, check whether the value is set corresponding to the name elements: True | False 
sismember (name, value) 

# 5, randomly remove and return an element of the specified collection 
SPOP (name) 
mumberR.spop = ( ' set_name ' ) 

# . 6, deleting an element collection 
Srem (name, value) 
r.srem ( " set_name " , " Tom " ) 

# . 7, obtaining intersection name corresponding to a plurality of sets of 
Sinter ( Keys, * args) 

r.sadd ( " set_name " , " A " , " B " ) 
r.sadd ( " set_name1 " , " B " , " C " ) 
r.sadd ( "set_name2", " B " , " C " , " D " ) 

Print (r.sinter ( " set_name " , " set_name1 " , " set_name2 " ))
 # Output: {b'b '} 

# . 8, acquiring a plurality of corresponding name collection of union 
SUNION (Keys, * args) 
r.sunion ( " set_name " , " set_name1 " , " set_name2 " )

 

Case: common concern Weibo

Demand: When the user accesses another user, the user will show two mutual concern over which the same user

Design: The user each user attention on the set, you can find the intersection

achieve:

  user001 = { 'peiqi', 'qiaozhi', 'danni'}

  user002 = { 'peiqi', 'qiaozhi', 'lingyang'}

  User001 and user002 common concern is:

  SINTER user001 user002

Result: { 'peiqi', 'qiaozhi'}

) # Common concern user_first and user_second human? ? Differencing set





= r.sinter Result ( ' user_first ' , ' user_second ' )
 # to set each element in the data string into types 
focus_on_set = SET ()
 for R & lt in Result: 
    focus_on_set.add (r.decode ()) 

Print (focus_on_set )

 

Guess you like

Origin www.cnblogs.com/maplethefox/p/11309466.html