2

I have a variable that contains a list of strings. For each string in the list I would like to create a new dictionary, then update each dictionary with that string value. This would result in 100 new dictionaries being created. For example my variable looks similar to this...

q)stock
"APPL"
"TSLA"
"AMZN"
"MSFT"
"NVDA"
..
q)count stock
100

I know how to do this for a single string in a variable...

q)newdict:()!()
q)stock:"AAPL"
q)newdict[`ticker]:stock
q)newdict
ticker|"AAPL"

But how would I do this for a variable that is a list of 100 strings? Also, if I wanted to update the dictionary to include additional key:value pairs from another variable with the same count (like below) what is the correct syntax to use for this operation?

q)date
2019.05.23T13:03:55.271474000 2019.05.23T13:03:55.271474000...
q)count date
100

Expected output for each string in variable would create a new dictionary similar to the following...

q)newdictAPPL
ticker|"APPL"
datetime|2019.05.23T13:03:55.271474000

q)newdictTSLA
ticker|"TSLA"
datetime|2019.05.23T13:05:33.727845200

q)newdictAMZN
ticker|"AMZN"
datetime|2019.05.23T13:08:27.742968000
2
  • Can you explain your expected output a little more? Do you want a table (which in kdb is simply a list of dictionaries)? Also, your stock variable contains 100 strings, not 100 characters (a string is a list of characters) Commented May 23, 2019 at 17:36
  • 1
    Thanks, Jonathan. Updated character to string and included an example of the expected outcome. A table would work fine as well, as long as I can then create individual dictionaries from that table and serialize each back to a json object. Commented May 23, 2019 at 18:07

1 Answer 1

3

I think what you're most likely to be interested in is a table. Starting with some sample data similar to what you have:

q)stock:20?("AAPL";"TSLA";"MSFT";"AMZN")
q)date:20?.z.Z
q)0N!stock;
("AMZN";"MSFT";"AMZN";"MSFT";"AAPL";"TSLA";"TSLA";"MSFT";"TSLA";"AAPL";"TSLA"..
q)date
2016.09.16T16:06:23.573 2010.10.04T23:28:53.863 2001.03.12T15:16:04.379 2005...

We can construct table quite simply in kdb:

q)t:([]ticker:stock;datetime:date)
q)t
ticker datetime               
------------------------------
"AMZN" 2016.09.16T16:06:23.573
"MSFT" 2010.10.04T23:28:53.863
"AMZN" 2001.03.12T15:16:04.379
"MSFT" 2005.07.17T04:02:58.577
"AAPL" 2012.12.17T10:48:15.839
"TSLA" 2017.09.16T11:06:02.579
"TSLA" 2002.11.18T00:03:57.945
"MSFT" 2009.06.02T08:28:32.680
"TSLA" 2013.10.24T06:50:31.420
"AAPL" 2007.06.12T07:04:33.058
"TSLA" 2006.08.10T03:45:58.748
"AAPL" 2001.01.17T11:04:57.387
"AAPL" 2010.08.29T21:47:39.564
"MSFT" 2003.10.19T01:58:58.820
"AMZN" 2010.11.21T00:05:03.256
"MSFT" 2001.05.13T21:03:21.293
"TSLA" 2004.02.13T07:49:57.013
"AAPL" 2015.01.31T08:13:03.986
"MSFT" 2009.05.24T06:34:05.044
"TSLA" 2013.03.28T22:11:03.641

We can see that a table in kdb is a list of dictionaries, we can index and get individual dictionaries:

q)t[0]
ticker  | "AMZN"
datetime| 2016.09.16T16:06:23.573
q)t[1]
ticker  | "MSFT"
datetime| 2010.10.04T23:28:53.863

We can also serialise to JSON using built-in .j.j function:

q).j.j t
"[{\"ticker\":\"AMZN\",\"datetime\":\"2016-09-16T16:06:23.573\"},{\"ticker\":..

Or if we want each dict as individual JSON strings:

q).j.j each t
"{\"ticker\":\"AMZN\",\"datetime\":\"2016-09-16T16:06:23.573\"}"
"{\"ticker\":\"MSFT\",\"datetime\":\"2010-10-04T23:28:53.863\"}"
"{\"ticker\":\"AMZN\",\"datetime\":\"2001-03-12T15:16:04.379\"}"
"{\"ticker\":\"MSFT\",\"datetime\":\"2005-07-17T04:02:58.577\"}"
"{\"ticker\":\"AAPL\",\"datetime\":\"2012-12-17T10:48:15.839\"}"
"{\"ticker\":\"TSLA\",\"datetime\":\"2017-09-16T11:06:02.579\"}"
"{\"ticker\":\"TSLA\",\"datetime\":\"2002-11-18T00:03:57.945\"}"
..
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Jonathon. That's what I was looking for! I'd give you an upvote buy my lack of "reputation" doesn't allow it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.