I’d like to create a multidimensional array in Python to parse it later on. I’m a switcher from PHP so I’m a little bit confused of how to create a dict or list or anything else to get that data out this way. Anyone an idea?
for group, value in data:
print '%s = %s' % (group, value)
for member, value in group:
print ' MEMBER: %s = %s' % (member, value)
>> 'Windows = 12%'
>> ' MEMBER: pc1 = 123'
>> ' MEMBER: pc2 = 321'
>> 'MacOS = 11%'
>> 'Linux = 13%'
>> ' MEMBER: pc3 = 213'
>> ' MEMBER: pc4 = 132'
>> ...
You can create a recursive list or dict to achieve it, I would prefer dict as you can name the variables e.g.
Now if you know your data structure is only 1 level deep, you can just iterate it like this
output:
But if you don’t know how much deep it is, e.g. members can further have more member which have more members and so on, you can recursively go thru it e.g.
output: