Redis zrange sort value as integer - redis

Since zrange "Lexicographical order is used for elements with equal score" how do I go around this issue?
For example:
zadd s 0 1
zadd s 0 2
zadd s 0 10
zadd s 0 3
zrange s 0 4
1) 1
2) 10
3) 2
4) 3
How do I make it sort like this (while ofc honor the score):
1) 1
2) 2
3) 3
4) 10

You cannot alter the lexicographic order.
However, you could store a value whose lexicographic order matches with numerical order. For instance instead of storing:
1
2
12
15
122
321
you could store:
A1
A2
B12
B15
C122
C321
The first letter is just a code to indicate the number of digits of the numerical value (A=1, B=2, etc ...), so that numerical and lexicographic order is the same. The client application can easily add/remove this prefix at store/retrieve time.
zadd s 0 A1
zadd s 0 A2
zadd s 0 B10
zadd s 0 A3
zrange s 0 4
1) "A1"
2) "A2"
3) "A3"
4) "B10"

Related

How to keep only the last index in groups of rows where a condition is met in pandas?

I have the following dataframe:
d = {'value': [1,1,1,1,1,1,1,1,1,1], 'flag_1': [0,1,0,1,1,1,0,1,1,1],'flag_2':[1,0,1,1,1,1,1,0,1,1],'index':[1,2,3,4,5,6,7,8,9,10]}
df = pd.DataFrame(data=d)
I need to perform the following filter on it:
If flag 1 and flag 2 are equal keep the row with the maximum index from the consecutive indices. Below for rows 4,5,6 and rows 9,10 flag 1 and flag 2 are equal. From the group of consecutive indices 4,5,6 therefore I wish to keep only row 6 and drop rows 4 and 5. For the next group of rows 9 and 10 I wish to keep only row 10. The rows where flag 1 and 2 are not equal should all be retained. I want my final output to look as shown below:
I am really not sure how to achieve what is required so I would be grateful for any advice on how to do it.
IIUC, you can compare consecutive rows with shift. This solution requires a sorted index.
In [5]: df[~df[['flag_1', 'flag_2']].eq(df[['flag_1', 'flag_2']].shift(-1)).all(axis=1)]
Out[5]:
value flag_1 flag_2 index
0 1 0 1 1
1 1 1 0 2
2 1 0 1 3
5 1 1 1 6
6 1 0 1 7
7 1 1 0 8
9 1 1 1 10

How to write the below given shorted set in Redis

How to write the below in Redis
"Create a sorted set myset1 with the following values ( 1, 'a', 2, 'b', 3 , 'c').Create a sorted set myset 2 with the following values (4, 'b', 1, 'c', 0, 'd').Find the intersection of myset1 and myset2 : - Write a command to find the intersection of myset1 and myset2 and store the intersection in out- Write a command to see the resulting intersection output Sorted set "out" : Find the union of myset1 and myset2 - Write a command to take the union of myset1 and myset2 and store the output in unout - Write a command to see the resulting intersection output Sorted set "unout" "
I wrote the code like below and it says 80% correct.
127.0.0.1:6379> zadd myset1 0 1 0 a 0 2 0 b 0 3 0 c (integer) 6 127.0.0.1:6379> zadd myset2 0 4 0 b 0 1 0 c 0 0 0 d (integer) 6 127.0.0.1:6379> zinterstore out 2 myset1 myset2 (integer) 3 127.0.0.1:6379> zrange out 0 -1 1) "1" 2) "b" 3) "c" 127.0.0.1:6379> zunionstore unout 2 myset1 myset2 (integer) 9 127.0.0.1:6379> zrange unout 0 -1 1) "0" 2) "1" 3) "2" 4) "3" 5) "4" 6) "a" 7) "b" 8) "c" 9) "d" 127.0.0.1:6379>
You can use ZINTERSTORE and ZUNIONSTORE commands to computer intersection and union of intersection of sorted sets.
ZINTERSTORE
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
Computes the intersection of numkeys sorted sets given by the
specified keys, and stores the result in destination. It is mandatory
to provide the number of input keys (numkeys) before passing the input
keys and the other (optional) arguments.
By default, the resulting score of an element is the sum of its scores
in the sorted sets where it exists. Because intersection requires an
element to be a member of every given sorted set, this results in the
score of every element in the resulting sorted set to be equal to the
number of input sorted sets.
ZUNIONSTORE
ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
Computes the union of numkeys sorted sets given by the specified keys,
and stores the result in destination. It is mandatory to provide the
number of input keys (numkeys) before passing the input keys and the
other (optional) arguments.
By default, the resulting score of an element is the sum of its scores
in the sorted sets where it exists.
Edit:
It's not accepting since you're adding score as key as well
Yout ZADD commands should be
ZADD myset1 1, a 2 b 3 c
zadd myset2 4 b 1 c 0 d
Try this
ZADD myset1 1 a 2 b 3 c
ZADD myset2 4 b 1 c 0 d
ZINTERSTORE out 2 myset1 myset2
ZUNIONSTORE unout 2 myset1 myset2

Inner Joins in redis

I have a problem that is very similar to one mentioned here...
https://grokbase.com/t/gg/redis-db/123wv39cnt/filtering-zset-by-hash-field-value
But I am not able to understand the answer provided in that thread.
zadd scores 1.0 mary
zadd scores 2.0 sue
zadd scores 3.0 bob
zadd scores 4.0 bruce
zadd scores 5.0 maggie
sadd females mary sue maggie
zinterstore femscores 2 scores females
zrange femscores 0 50 withscores
How are the values 2, 3 and 6 calculated?
1) "mary"
2) "2"
3) "sue"
4) "3"
5) "maggie"
6) "6"
It is the result of the default WEIGHTS and AGGREGATE as described in ZUNIONSTORE.
Default WEIGHTS is 1, default AGGREGATE is SUM, so you are seeing the scores incremented by 1.
If you want the scores score without modification, simply set to zero the weight for females:
ZINTERSTORE femscores 2 scores females WEIGHTS 1 0

From redis sorted set, retrieving the rank of the highest value that has score just less than given score

From a redis sorted set, how do I retrieve the rank of highest value that has a score just less than a given score?
For instance, imagine my sorted set is:
rank value score
1) 'a' -10
2) 'd' -4
3) 'c' 0
4) 'b' 2
5) 'e' 10
Specifically, If I'm given the score 12, I want to retrieve rank 5. If I'm given the score 1, I want to retrieve rank 3. If I'm given a score -11, I want to retrieve nothing.
Note #1: rank in a Sorted Set is 0-based
Note #2: you'll have to do two queries, one for finding the element and the other for obtaining its rank.
Example using redis-cli:
127.0.0.1:6379> ZADD z -10 a -4 d 0 c 2 b 10 e
(integer) 5
127.0.0.1:6379> ZREVRANGEBYSCORE z (12 -inf LIMIT 0 1
1) "e"
127.0.0.1:6379> ZRANK z e
(integer) 4
127.0.0.1:6379> ZREVRANGEBYSCORE z (1 -inf LIMIT 0 1
1) "c"
127.0.0.1:6379> ZRANK z c
(integer) 2
127.0.0.1:6379> ZREVRANGEBYSCORE z (-11 -inf LIMIT 0 1
(empty list or set)
Naturally, a Lua script would be ideal for this case, i.e:
$ cat script.lua
local r=redis.call('ZREVRANGEBYSCORE', KEYS[1], '('..ARGV[1], '-inf', 'LIMIT', 0, 1)
if #r > 0 then
r=redis.call('ZRANK', KEYS[1], r[1])
end
return r
$ redis-cli --eval script.lua z , 12
(integer) 4
$ redis-cli --eval script.lua z , 1
(integer) 2
$ redis-cli --eval script.lua z , -11
(empty list or set)

How to calculate the number of pairs in an Excel spreadsheet?

I have two columns of integers between 1 and 16 in an excel file. I'd like to count the number of pairs of integers in these columns. There are 256 cases and I'd like to have a column which tells me how many pairs exist for each case. For instance, I have a table like below:
1 2
1 1
1 3
1 4
1 1
1 8
1 1
16 16
1 2
...
And I'd like to calculate a column like this:
3 (number of 1 1s)
2 (number of 1 2s)
1 (number of 1 3s)
1 (number of 1 4s)
0 (number of 1 5s)
0 (number of 1 6s)
0 (number of 1 7s)
1 (number of 1 8s)
...
1 (number of 16 16s)
I'd appreciate if someone can help me with the calculation.
First you need to create two columns with all possible combinations:
1 1
1 2
1 3
...
2 1
2 2
...
16 16
Let's assume these are in columns C,D and your data are in columns A, B, in rows 1 to 1000. Then you can use an array formula:
=SUM(IF(($A$1:$A$1000=C1)*($B$1:$B$1000=D1);1;0))
You must press Shift+Ctrl+Enter when entering array formula.