KDB C++ API: creating a list of strings - c++

I am failing to create a list of strings in KDB C++ API. This is what I am doing:
K lst = ktn(KC,0);
jk(&lst, kp((S)"str_1"));
jk(&lst, kp((S)"str_2"));
r1(lst);
k(h, (S)"{show type x}" , lst, (K)0);
r1(lst);
k(h, (S)"{show count x}", lst, (K)0);
r1(lst);
k(h, (S)"{show x}" , lst, (K)0);
The output
10h
2
"\260\260"
Where instead of "\260\260" could be some other random string repeated twice. It seems, I do create a list of strings, but the list contains rubbish for whatever reason. Could you please help me to understand what I am doing wrong here?
Thank you for your help!

You're initialising the list with type char (KC), so this will be a char list, not a string list.
A string list is a mixed type list, as it a list of list of chars in Kdb. Therefore, you need to initialise the list with type 0 (mixed list type).
Example C code - strList.c:
#define KXVER 3
#include "k.h"
K createStrListStatic(K x){
K strList = ktn(0,2);
kK(strList)[0] = kp("hello");
kK(strList)[1] = kp("world");
return strList;
}
K createStrListDynamic(K x){
K strList = ktn(0,1);
kK(strList)[0] = kp("hello");
js(&strList, (S)kp("world"));
return strList;
}
Example Q code to load the lib & call C funcs - strList.q:
createStrListStatic:`:strList 2:(`createStrListStatic;1);
createStrListDynamic:`:strList 2:(`createStrListDynamic;1);
-1 "\ncreateStrListStatic";
-1 "\t",.Q.s1 createStrListStatic[];
-1 "\ncreateStrListDynamic";
-1 "\t",.Q.s1 createStrListDynamic[];
Compile and run as follows:
$ gcc -shared -m32 -fPIC strList.c -o strList.so && LD_LIBRARY_PATH="." q strList.q
KDB+ 3.4 2016.10.27 Copyright (C) 1993-2016 Kx Systems
l32/ 8()core 7982MB salih glyph01 127.0.1.1 NONEXPIRE
createStrListStatic
("hello";"world")
createStrListDynamic
("hello";"world")

Related

In maple, how to create a sequence of alternating variables?

I'm quite new to Maple and I would like to create the following list:
U__N := u__1[0], u__2[0], u__1[1], u__2[1], u__1[2], u__2[2], u__1[3], u__2[3], u__1[4], u__2[4], u__1[5], u__2[5]
I came up with the following two options. For both I lack the knowledge for the last step
Option 1
U__N := seq([u__1[k], u__2[k]], k = 0 .. 5)
which gives me a nested list: U__N := [u__1[0], u__2[0]], [u__1[1], u__2[1]], [u__1[2], u__2[2]], [u__1[3], u__2[3]], [u__1[4], u__2[4]], [u__1[5], u__2[5]]. However, now I do not know how to "un-nest" the nested list.
Option 2:
Create two separate lists
U__N2 := seq(u__2[k], k = 0 .. 5 - 1)
which returns U__N1 := u__1[0], u__1[1], u__1[2], u__1[3], u__1[4]
U__N2 := seq(u__2[k], k = 0 .. 5 - 1)
which returns U__N2 := u__2[0], u__2[1], u__2[2], u__2[3], u__2[4].
Now I would like to concatenate/combine these two lists alternatively.
Do you have any suggestions for one of these two options or an alternative solution?
I would prefer to create the pair-wise portions and then utilize those directly, than to form the whole list-of-lists and Flatten it.
The special-evaluation rules of the seq command allows for its first argument to not be evaluated until after k attains concrete numeric values.
This allow you to adjust your first method and extract the operands of the pair-wise inner lists, using the op command.
seq(op([u__1[k], u__2[k]]), k = 0 .. 5);
u__1[0], u__2[0], u__1[1], u__2[1],
u__1[2], u__2[2], u__1[3], u__2[3],
u__1[4], u__2[4], u__1[5], u__2[5]
seq([u__1[k], u__2[k]][], k = 0 .. 5);
u__1[0], u__2[0], u__1[1], u__2[1],
u__1[2], u__2[2], u__1[3], u__2[3],
u__1[4], u__2[4], u__1[5], u__2[5]
The trailing [] in [...][] acts like op([...]).
using Flatten gave me the desired solution

nested list of lists of inegers - doing arithmetic operation

I have a list like below and need to firs add items in each list and then multiply all results 2+4 = 6 , 3+ (-2)=1, 2+3+2=7, -7+1=-6 then 6*1*7*(-6) = -252 I know how to do it by accessing indexes and it works (as below) but I also need to do it in a way that it will work no matter how many sublist there is
nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]]
a= nested_lst[0][0] + nested_lst[0][1]
b= nested_lst[1][0] + nested_lst[1][1]
c= nested_lst[2][0] + nested_lst[2][1] + nested_lst[2][2]
d= nested_lst[3][0] + nested_lst[3][1]
def sum_then_product(list):
multip= a*b*c*d
return multip
print sum_then_product(nested_lst)
I have tried with for loop which gives me addition but I don't know how to perform here multiplication. I am new to it. Please, help
nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]]
for i in nested_lst:
print sum(i)
Is this what you are looking for?
nested_lst = [[2,4], [3,-2],[2,3,2], [-7,1]] # your list
output = 1 # this will generate your eventual output
for sublist in nested_lst:
sublst_out = 0
for x in sublist:
sublst_out += x # your addition of the sublist elements
output *= sublst_out # multiply the sublist-addition with the other sublists
print(output)

Python remove all numbers from a list

I have a list of unicode elements and I'm trying to remove all integer numbers from It.
My code is:
List = [u'123', u'hello', u'zara', u'45.3', u'pluto']
for el in List:
if isinistance(el, int):
List.remove(el)
The code doesn't work, It give me the same list with u'123' include.
What i need is this:
List = [ u'hello', u'zara', u'45.3', u'pluto']
Can somebody hel me?
You list consists of unicode strings which are no instances of int obviously. You can try converting them to int in a helper function and use this as a condition to remove them/ to construct a new list.
def repr_int(s):
try:
int(s)
return True
except ValueError:
return False
original_list = [u'123', u'hello', u'zara', u'45.3', u'pluto']
list_with_removed_ints = [elem for elem in original_list if not repr_int(elem)]

TypeError: list indices must be integers, not unicode in python code

I used the split() function to convert string to a list time = time.split() and this is how my output looks like :
[u'1472120400.107']
[u'1472120399.999']
[u'1472120399.334']
[u'1472120397.633']
[u'1472120397.261']
[u'1472120394.328']
[u'1472120393.762']
[u'1472120393.737']
Then I tried accessing the contents of the list using print time[1] which gives the index out of range error (cause only a single value is stored in one list). I checked questions posted by other people and used print len(time). This is the output for that:
1
[u'1472120400.107']
1
[u'1472120399.999']
1
[u'1472120399.334']
1
[u'1472120397.633']
1
[u'1472120397.261']
1
[u'1472120394.328']
1
[u'1472120393.762']
1
[u'1472120393.737']
I do this entire thing inside a for loop because I get logs dynamically and have to extract out just the time.
This is part of my code:
line_collect = lines.collect() #spark function
for line in line_collect :
a = re.search(rx1,line)
time = a.group()
time = time.split()
#print time[1] #index out of range error which is why I wrote another for below
for k in time :
time1 = time[k]#trying to put those individual list values into one variable but get type error
print len(time1)
I get the following error :
time1 = time[k]
TypeError: list indices must be integers, not unicode
Can someone tell me how to read each of those single list values into just one list so I can access each of them using a single index[value]. I'm new to python.
My required output:
time =['1472120400.107','1472120399.999','1472120399.334','1472120397.633','1472120397.261','1472120394.328','1472120393.762','1472120393.737']
so that i can use time[1] to give 1472120399.999 as result.
Update: I misunderstood what you wanted. You have the correct output already and it's a string. The reason you have a u before the string is because it's a unicode string that has 16 bits. u is a python flag to distinguish it from a normal string. Printing it to the screen will give you the correct string. Use it normally as you would any other string.
time = [u'1472120400.107'] # One element just to show
for k in time:
print(k)
Looping over a list using a for loop will give you one value at a time, not the index itself. Consider using enumerate:
for k, value in enumerate(time):
time1 = value # Or time1 = time[k]
print(time1)
Or just getting the value itself:
for k in time:
time1 = k
print(time1)
--
Also, Python is zero based language, so to get the first element out of a list you probably want to use time[0].
Thanks for your help. I finally got the code right:
newlst = []
for line in line_collect :
a = re.search(rx1,line)
time = a.group()
newlst.append(float(time))
print newlst
This will put the whole list values into one list.
Output:
[1472120400.107, 1472120399.999, 1472120399.334, 1472120397.633,
1472120397.261, 1472120394.328, 1472120393.762, 1472120393.737]

Accessing R list elements through function parameters

I have an R list which looks as follows
> str(prices)
List of 4
$ ID : int 102894616
$ delay: int 8
$ 47973 :List of 12
..$ id : int 47973
..$ index : int 2
..$ matched: num 5817
$ 47972 :List of 12
..
Clearly, I can access any element by e.g. prices$"47973"$id.
However, how would I write a function which parametrises the access to that list? For example an access function with signature:
access <- function(index1, index2) { .. }
Which can be used as follows:
> access("47973", "matched")
5817
This seems very trivial but I fail to write such a function. Thanks for any pointers.
Using '[[' instead of '$' seems to work:
prices <- list(
`47973` = list( id = 1, matched = 2))
access <- function(index1, index2) prices[[index1]][[index2]]
access("47973","matched")
As to why this works instead of:
access <- function(index1, index2) prices$index1$index2 (which I assume is what you tried?) it's because here index1 and index2 are not evaluated. That is, it searches the list for an element called index1 instead of what this object evaluates to.
You can take advantage of the fact that [[ accepts a vector, used recursively:
prices <- list(
`47973` = list( id = 1, matched = 2))
prices[[c("47973", "matched")]]
# 2