0

There is a function in kdb generating multiple tables as results. My main aim is to analyze each of the table generated using python.

Is there a way I can make it return a list of table or some dictionary of tables so I can export it to python or should I try something else? Any leads on this is appreciated. Thanks

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36

1 Answers1

3

It's not clear what you're asking - do you have a function which generates multiple tables? And you want to return a list of these tables? If that is the case and you have something like

f:{t:([]a:x?`1;b:x?10);q:([]c:x?`2;d:x?10f)}

that you want to modify to return both t and q, you can just manually construct a list from within the function:

q)f:{t:([]a:x?`1;b:x?10);q:([]c:x?`2;d:x?10f);(t;q)}
q)f 3
+`a`b!(`o`p`l;5 8 5)
+`c`d!(`ig`nf`no;9.149882 9.030751 7.750292)

or alternatively, you can return the tables by using enlist on each table and joining the results of these operations:

q)f:{t:([]a:x?`1;b:x?10);q:([]c:x?`2;d:x?10f);enlist[t],enlist[q]}
q)f 3
+`a`b!(`n`a`a;6 9 0)
+`c`d!(`nn`mi`om;9.216436 1.809536 6.434637)

Or, if you use each to pass multiple inputs to a function returning a single table, the results will naturally be a list of tables:

q)f:{t:([]a:x?`1;b:x?10)}
q)f each 3 3
+`a`b!(`l`o`d;9 5 2)
+`a`b!(`h`m`g;9 5 9)

One other modification you could make is to join each table onto a table list as it's created:

q)f:{tl:();
     t:([]a:x?`1;b:x?10);
     tl,:enlist t;
     q:([]c:x?`2;d:x?10f);
     tl,:enlist q;
     tl}
q)f 3
+`a`b!(`a`l`i;1 9 1)
+`c`d!(`db`mi`la;2.371288 5.67081 4.269177)

This is quite verbose with assigning tables, but you don't need to do so directly, you could also do:

 q)f:{tl:();
      tl,:enlist ([]a:x?`1;b:x?10);
      tl,:enlist ([]c:x?`2;d:x?10f);
      tl}

To get the same output, joining each table to the list as it's created, and so returning a list of tables.

Ryan McCarron
  • 889
  • 4
  • 10
  • it's a bit clearer now - how are you importing these back into python? Are you trying to save to text files and import or are you using a kdb/python interface? – Ryan McCarron Jan 17 '18 at 14:10
  • I have a kdb interface which can export a table to xls file –  Jan 17 '18 at 14:14
  • it sounds like you might just need one of the first two methods above - assign each table and make a list at the end. There's one other modification you could make as well, I'll edit it in now – Ryan McCarron Jan 17 '18 at 14:16
  • No problem! It seems like your main focus is python, you should consider checking out https://training.aquaq.co.uk/ . I work for AquaQ now, just for full disclosure, but I did these courses when training, and it's very useful for getting a good foundation in kdb, even just the free course. – Ryan McCarron Jan 17 '18 at 14:51
  • just one more ques. if the number of columns are different in tables the method doesn't seem to work. is there any approach for that? I have just started watching aquaq videos though –  Jan 17 '18 at 14:57
  • getting this error : ERROR: 'length (incompatible lengths (different lengths of operands for synchronized operation or table columns lengths are not the same) –  Jan 17 '18 at 14:57
  • The above should still work - the list of tables is a mixed list, so it should't really matter what you're joining on to the end, like here where the second table has three columns: ```f:{tl:();tl,:enlist ([]a:x?`1;b:x?10);tl,:enlist ([]c:x?`2;d:x?10f;e:x?100);tl};f 3 +`a`b!(`k`c`h;4 4 2) +`c`d`e!(`ha`oo`fl;2.011578 0.6832366 5.989167;2 96 93)``` maybe you're attempting to make a dictionary and the length of keys don't match the length of values or something like that. – Ryan McCarron Jan 17 '18 at 15:09