List arrangements in python from loops -


my code this:

j=0 list1=[]  port in portlist1:     l=[[port.getname(),port.getsize()]]     register in port.getregisters():         j=j+1     l.append(j)     list1.append(l)     j=0 

output print list1 is:

      [[['b', 10], 2], [['c', 25], 1], [['f', 30], 0]] 

what changes should if want use functions

      register.getaddress(),register.getdirection() inside 2nd loop instead of increment j. 

so output should be:

 print list1:      [[['b', 10], [['1000',in],['1',out]]], [['c', 25], ['v', 1001]], [['f', 30], []]]  print list1[0]:   [['b', 10], [['1000',in],['1',out]]  print list1[0][1]:  ['1000',in],['1',out] 

i think can list comprehension:

list1 = []  port in portlist1:     l = [[port.getname(),port.getsize()],          [[register.getaddress(), register.getdirection()]           register in port.getregisters()]]     list1.append(l) 

however, gives nightmarishly deep nested list. think babu's comment using dictionary right on. here's how might improve things way:

dict1 = {} port in portlist1:     dict1[port.getname()] = {"size": port.getsize(),                              "registers": {register.getaddress(): register.getdirection()                                            register in port.getregisters}} 

Comments