redis basis of an ordered collection of application

'' ' Music Player ranking ' '' 
Import Redis 

R & lt = redis.Redis (Host = ' 127.0.0.1 ' , Port = 6379, password = ' 123456 ' , DB = 0) 
r.zadd ( ' Ranking ' , { ' song1 ' :. 1, ' song2 is ' :. 1, ' song3 ' :. 1, ' song4 ' :. 1 }) 
r.zadd ( ' Ranking ' , { ' song5 ' :. 1, 'song6': 1, 'song7': 1})
r.zadd('ranking', {'song8': 1, 'song9': 1})
r.zincrby('ranking', 50, 'song3')
r.zincrby('ranking', 60, 'song5')
r.zincrby('ranking', 80, 'song7')
res = r.zrevrange('ranking' , 0, 2, withscores = True) 
I =. 1
 for J in RES:
     Print ( ' first name is {}: {amount} Play: {Ci} ' .format (I, J [0] .decode (), int (J [. 1 ]))) 
    I + =. 1 


output results: 
The first one is: song7 playback volume: 81 
No. 2 is: song5 playback volume: 61 times 
of three is: song3 playback volume: 51 times

 

 

'' ' Jingdong trade bestseller ' '' 
Import Redis 

R & lt = redis.Redis (Host = ' 127.0.0.1 ' , Port = 6379, DB = 0, password = 123456 ) 
r.flushdb () 
day01_dict = { ' HUAWEI ' : 5000, ' OPPO ' : 4000, ' iPhone ' : 3000 } 
day02_dict = { ' HUAWEI ' : 5200, ' OPPO ' : 4300, ' iPhone ' :3230}
day03_dict = {'huawei': 5500, 'oppo': 4660, 'iphone': 3580}
r.zadd('mobile_day01', day01_dict)
r.zadd('mobile_day02', day02_dict)
r.zadd('mobile_day03', day03_dict)
r.zunionstore('mobile_day01:03', ('mobile_day01', 'mobile_day02', 'mobile_day03'), aggregate='max')
RES = r.zrevrange ( ' mobile_day01: 03 ' , 0, -1, withscores = True)
 Print (RES) 
I =. 1
 for R & lt in RES:
     Print ( ' first name} {: {} ' .format (I, R & lt [0] .decode ())) 
    I + =. 1 


output results: 
[(B ' HUAWEI ' , 5500.0), (B ' OPPO ' , 4660.0), (B ' iPhone ' , 3580.0 )] 
of 1: huawei 
of 2: oppo 
first three: iphone

 

Guess you like

Origin www.cnblogs.com/yuxiangyang/p/11199377.html