Conversion from an array of python dictionaries to an array of arrays using transform

Hi all,

I'm currently working on trying to take a return type of an array of python dictionaries to and array of arrays using transforms. However, I have thus far been unsuccessful. Does anyone know a way to do this using any of the base 4 transforms?

image

Thank you,

Cam

Can you give an example of your input data and how you would like it to look after the transform? If the keys aren't the same in each dictionary I suspect that you will be in trouble.

2 Likes

If I'm understanding you correctly, you can just loop through the dictionaries in the array, and then you can loop through the keys in each dictionary. Something like this might work:

def convertData(data):	
	return [[dic[key] for key in dic] for dic in data]
1 Like

That part can be shortened to just:

	return [d.values() for d in data]
1 Like

You gentlemen are ingenious.

Thank you

Do be aware that key order in jython dictionaries is unspecified, and may not match your expectations. If you need keys pulled in a specific order, you need smarter code.

Fair enough. How would I go about editing it so it returns it in order then?

You need a list of the keys you want in the order you want them. Then use Daniel's construct but using the desired keys in the inner loop instead of the keys in the order the dict() supplies.

Okay, that would make sense. Just for my own understanding @Daniel.Snyder solution is more like an enumeration type where the 'key' acts as the index and 'dic' acts as the value type, correct?

dic is short for dict, but using dict as a variable name breaks things (it is the name of a built-in in python).

1 Like

Okay, that would make sense. I appreciate your help and explanation :smile:

[[dic[key] for key in dic] for dic in data]

is practically equivalent to

l=[] 
for dic in data:
   ll=[] 
   for key in dic:
      ll.append(dic[key]) 
   l.append(ll)

A list comprehension will run slightly faster though

If you want it to retain order the easy jython way is to use an ordered dict.

from collections import OrderedDict

d = OrderedDict()
d['someKey'] = 'someValue'
# etc

and then iterating though this dictionary with for k, v in d.items() will run through them in the order that you added the key-value pairs.

That won't survive assignment to a Perspective property. It will come back out unordered. If ordered keys are critical and not known ahead of time, use a single-row dataset instead of a map.

(My Simulation Aids V2 columnsOf() function returns an ordered Java map that holds its ordering within the expression using it, but loses the order when placed in a Perspective property. Perspective's property wrappers are an ungodly mess.)

4 Likes