I have a list of Strings in redis -
LPUSH keys 1 2 3 4
And reading is pretty easy -
LRANGE keys 0 3
1) "4"
2) "3"
3) "2"
4) "1"
How can I read from list where each value has some specified string prepended to it? In above scenario I want my output as -
1) "Key:4"
2) "Key:3"
3) "Key:2"
4) "Key:1"
You will need to use lua - https://redis.io/commands/eval
You can search for lua documentation and modify the code below according your needs.
Here is an example:
127.0.0.1:6379> LPUSH keys 1 2 3 4
(integer) 4
127.0.0.1:6379> LRANGE keys 0 3
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> EVAL 'local res = {} local ttt=redis.call("LRANGE", "keys", "0", "10") for k, v in pairs(ttt) do table.insert(res, "Key:" .. v) end return res' 0
1) "Key:4"
2) "Key:3"
3) "Key:2"
4) "Key:1"
Related
I have data like (("generic" 7) ("ore" 1) ("generic" 4) ("wood" 6) ("wheat" 3) ("generic" 2) ("generic" 9) ("sheep" 5) ("brick" 8))
and I want to turn it into
{"brick" 1
"generic" 4
"sheep" 1
"ore" 1
"wheat" 1
"wood" 1}
Since "generic" appears 4 times in the data, I want the key to be "generic" and the value to be 4. Since "sheep" appears 1 time in the data, I want the key to be "sheep" and the value to be "1".
Use map, first and frequencies:
(->> '(("generic" 7) ("ore" 1) ("generic" 4) ("wood" 6) ("wheat" 3) ("generic" 2) ("generic" 9) ("sheep" 5) ("brick" 8))
(map first)
frequencies)
=> {"generic" 4, "ore" 1, "wood" 1, "wheat" 1, "sheep" 1, "brick" 1}
(->> is "thread-last" macro.)
I have sorted list in tcl and i am iterating over it using foreach
During iteration i wish to append an item to the list and also make sure that item goes at the right place in the sorting order.
Something like this:
foreach item $mylist {
..
if { $sun == "shining" } {
set mylist [lsort [lappend mylist "newitem"]]
continue
}
..
}
Is it a valid thing in tcl. Will this work as expected under all cases & circumstances?
foreach will expand the $mylist variable once, before starting to iterate:
% set mylist {1 2 3 4 5}
1 2 3 4 5
% foreach item $mylist { puts $item; lappend mylist "$item$item" }
1
2
3
4
5
% set mylist
1 2 3 4 5 11 22 33 44 55
So, it depends on what you expect when you say "does it work as expected"
As glenn suggested, you can use a while whose test condition is expressed over the state of the list (e.g. using llength).
set mylist {1 2 3 4 5}
while {[llength $mylist]} {
# consume first list element
set mylist [lassign $mylist item]
if {$item <= 5} {
lappend mylist $item$item
}
}
Note that lassign turns out useful to consume the list. It goes without saying, still saying, that you need some stop condition (e.g., the list must deplete at some point, or you break explicitly).
I have this list: l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] and I would like to iterate on it, printing something like
0: [1,2,3,4]
1: [2,3,4,5]
2: [3,4,5,6]
...
n: [17,18,19,20]
So far I made this code to print 5 elements at a time, but the last iteration prints 3:
for index, item in enumerate(l):
if index == 0 or index == 1 or index == 2:
continue
print index, l[index - 3:index + 2]
How can I solve this?
You're on the right track with your list slices. Here's a tweak to make it easier:
sub_len = 4
for i in range(len(mylist) - sub_len + 1):
print l[i:i + sub_len]
Where sub_len is the desired length of the slices you print.
demo
I want to delete some rows in pandas dataframe.
ID Value
2012XY000 1
2012XY001 1
.
.
.
2015AB000 4
2015PQ001 5
.
.
.
2016DF00G 2
I want to delete rows whose ID does not start with 2015.
How should I do that?
Use startswith with boolean indexing:
print (df.ID.str.startswith('2015'))
0 False
1 False
2 True
3 True
4 False
Name: ID, dtype: bool
print (df[df.ID.str.startswith('2015')])
ID Value
2 2015AB000 4
3 2015PQ001 5
EDIT by comment:
print (df)
ID Value
0 2012XY000 1
1 2012XY001 1
2 2015AB000 4
3 2015PQ001 5
4 2015XQ001 5
5 2016DF00G 2
print ((df.ID.str.startswith('2015')) & (df.ID.str[4] != 'X'))
0 False
1 False
2 True
3 True
4 False
5 False
Name: ID, dtype: bool
print (df[(df.ID.str.startswith('2015')) & (df.ID.str[4] != 'X')])
ID Value
2 2015AB000 4
3 2015PQ001 5
Use str.match with regex string r'^2015':
df[df.ID.str.match(r'^2015')]
To exclude those that have an X afterwards.
df[df.ID.str.match(r'^2015[^X]')]
The regex r'^2015[^X]' translates into
^2015 - must start with 2015
[^X] - character after 2015 must not be X
consider the df
then
df[df.ID.str.match(r'^2015[^X]')]
i trying to assign a row number and a Set-number for List, but Set Number containing wrong number of rows in one set.
var objx = new List<x>();
var i = 0;
var r = 1;
objY.ForEach(x => objx .Add(new x
{
RowNumber = ++i,
DatabaseID= x.QuestionID,
SetID= i == 5 ? r++ : i % 5 == 0 ? r += 1 : r
}));
for Above code like objY Contains 23 rows, and i want to break 23 rows in 5-5 set.
so above code will give the sequence like[Consider only RowNumber]
[1 2 3 4 5][6 7 8 9][ 10 11 12 13 14 ].......
its a valid as by the logic
and if i change the logic for Setid as
SetID= i % 5 == 0 ? r += 1 : r
Result Will come Like
[1 2 3 4 ][5 6 7 8 9][10 11 12 13 14].
Again correct output of code
but expected for set of 5.
[1 2 3 4 5][ 6 7 8 9 10].........
What i missing.............
i should have taken my Maths class very Serious.
I think you want something like this:
var objX = objY.Select((x, i) => new { ObjX = x, Index = i })
.GroupBy(x => x.Index / 5)
.Select((g, i) =>
g.Select(x => new objx
{
RowNumber = x.Index + 1
DatabaseID = x.ObjX.QuestionID,
SetID = i + 1
}).ToList())
.ToList();
Note that i'm grouping by x.Index / 5 to ensure that every group has 5 items.
Here's a demo.
Update
it will be very helpful,if you can explain your logic
Where should i start? I'm using Linq methods to select and group the original list to create a new List<List<ObjX>> where every inner list has maximum 5 elements(less in the last if the total-count is not dividable by 5).
Enumerable.Select enables to project something from the input sequence to create something new. This method is comparable to a variable in a loop. In this case i project an anonymous type with the original object and the index of it in the list(Select has an overload that incorporates the index). I create this anonymous type to simply the query and because i need the index later in the GroupBy``.
Enumerable.GroupBy enables to group the elements in a sequence by a specified key. This key can be anything which is derivable from the element. Here i'm using the index two build groups of a maximum size of 5:
.GroupBy(x => x.Index / 5)
That works because integer division in C# (or C) results always in an int, where the remainder is truncated(unlike VB.NET btw), so 3/4 results in 0. You can use this fact to build groups of the specified size.
Then i use Select on the groups to create the inner lists, again by using the index-overload to be able to set the SetId of the group:
.Select((g, i) =>
g.Select(x => new objx
{
RowNumber = x.Index + 1
DatabaseID = x.ObjX.QuestionID,
SetID = i + 1
}).ToList())
The last step is using ToList on the IEnumerable<List<ObjX>> to create the final List<List<ObX>>. That also "materializes" the query. Have a look at deferred execution and especially Jon Skeets blog to learn more.