views:

62

answers:

1

Hi

I am having problems with creating a matplot lib table using python.

The example i have been following : http://matplotlib.sourceforge.net/examples/pylab_examples/table_demo.html

My data :

I have two for loops where in the first For loop i get the Protocol Name and in the second for loop i get the module name. I need the data to be like this for pushing it into the matplotlib table.

data = [[  66386,  174296,   75131,  577908,   32015],
        [  58230,  381139,   78045,   99308,  160454]]

Instead my Data is like this

data = [[  66386,  174296,   75131,  577908,   32015,
          58230,  381139,   78045,   99308,  160454]],

My Code :

ProtocolList = [DHCP , PPOEE ,WANe ]
  passList = []

for protocol in protocolList:

           # Comment- modules= [ frag,app,scale] This below line gets all the module names

           moduleNames = d[ protocol ].getModuleNames()  

            for module in moduleNames:

                 # This line gets all the passes for each module 
                 numberOfPasses = d[protocol].getModule(module)[1]                 
                 passList.append(numberOfPasses)
 print passList


My Output : [6, 3, 1, 2, 22, 3, 3, 4, 13, 0, 7, 27, 5, 8, 4, 8, 1, 7, 6, 13, 7, 9, 23, 3, 4,
1, 7, 4, 0, 22, 8, 6, 7, 27, 4, 3, 1, 3, 1, 12, 3, 6, 8, 2, 9, 19, 3, 6, 2, 22,
2, 8, 4, 3, 0, 7, 10, 27, 5, 8, 4, 1, 7, 6, 13, 1, 9, 23, 3, 4]

Its Combining all the passes all modules in one list where i want the passes for each module to go into individual lists so i can put it use it for the matplotlib table data

So basically i have to put the pass and fail values in individual lists

like this [[ pass] [fail] [pass] [fail] ] Instead its like this [[ pass , pass , fail , fail ]]

A: 

You're using a single append statement. Since you're doing that it's only ever going to append to a single list.

passList.append( d[ protocal].getModule(module)[1] for module in moduleNames )

that'll create a new list for each protocal which isn't what you want. One sec...

from collections import defaultdict
passList = defaultdict(list)

for module in moduleNames:
    passList[ module ].append( d[ protocol].getModule( module )[1] )

print passList.values()
wheaties
Thanks Please look at the Code . Dont look at the description . I need the Passes to be in individual List [ [ pass ] [pass [ pass ] ] Example is : Protocol [ dhcp , WAN , PPOE ] Module names [ frag , scale , basic ] . So for each protocol i go into the forloop , then i get the all the modules for that particular protocol and i get the passes for each module. there can be 6 modules for each protocol. again i do this for individual protocols. similarly i get it for all protocols
John
Hi thanks for the answer , I am getting the error " collections.defaultdict' object has no attribute 'append' "
John
Are you just doing `passList.append` or are you doing `passList[ key ].append( value )`? If the former, you need to tell it which key (module in your case) to map to which value (a list). If the later, it's working fine here.
wheaties
Thanks for ur answer it worked Bascially i have it like this [[ pass] [pass ] [fail ] [fail ] [block ] [block ] can i have it like this ? [ [ pass ] [fail ] [block ] [pass] [fail ] [block ]] the Code for Fail and blocked : for module in moduleNames: failList[ module ].append( d[ protocol].getModule( module )[0] ) for module in moduleNames: blockList[ module ].append( d[ protocol].getModule( module )[2] )
John