help on Implementing a list of dicts with no data pattern

Dave Angel davea at davea.name
Thu May 9 18:32:21 EDT 2013


On 05/09/2013 05:22 PM, rlelis wrote:
> On Thursday, May 9, 2013 7:19:38 PM UTC+1, Dave Angel wrote:
>
> Yes it's a list of string. I don't get the NameError: name 'file_content' is not defined in my code.

That's because you have the 3 lines below which we hadn't seen yet.

>
> After i appended the headers i wanted to cut the data list it little bit more because there was some data (imagine some other collumns) to the left that didn't needed.
>
> file_content = []
> for d in data:
>      file_content.append(d[1:])
>
> from this point on i've showed the code,
> highway_dict = {}
> aging_dict = {}
> queue_counters={}
> queue_row = []
> for content in file_content:
>          if 'aging' in content:
>                  # aging 0 100
>                  # code here
>

OK, so I now have some code I can actually run.  Unfortunately, it 
produces an error:

Traceback (most recent call last):
   File "ricardo.py", line 23, in <module>
     aging_dict['total'], aging_dict[columns] = total, aging_values
NameError: name 'total' is not defined

So I'll make a reasonable guess that you meant total_values there.  I 
still can't understand how you're testing this code, when there are 
trivial bugs in it.

Next, I get:

Traceback (most recent call last):
   File "ricardo.py", line 32, in <module>
     highway_dict['lanes'], highway_dict['state'], 
highway_dict['limit(mph)'] = lanes, state, limit_values
NameError: name 'lanes' is not defined

and then:

Traceback (most recent call last):
   File "ricardo.py", line 32, in <module>
     highway_dict['lanes'], highway_dict['state'], 
highway_dict['limit(mph)'] = lanes_values, state, limit_values
NameError: name 'state' is not defined

Each of those not-defined errors was pointed out by me earlier in the 
thread.

I don't see any output logic, so I guess it's up to us to guess what the 
meanings and scope of the various lists and dicts are.  I figure the 
queue_row is your final collection that you hope to get results from. 
It's a list containing many references to a single queue_counters 
object.  So naturally, they all look the same.

If you want them to be different, you have to create a new one each 
time.  Move the line:
     queue_counters={}

inside the loop, right after the line:
     for content in file_content:

There are a bunch of other things wrong, like not lining up the columns 
when you're substringing content, but this may be your major stumbling 
block.  Note:  you may have to also move the highway_dict and 
aging_dict;  I haven't figured out what they're for, yet.

Following is the code I've been using to try to figure out what you were 
intending:


file_content = [
         "aging 0 100",
         "aging 2 115",
         "aging 3 1",
         "highway 4 disable 25",
         "highway 2 disable 245",
         "highway 0 enable 125",
     ]

highway_dict = {}
aging_dict = {}
#queue_counters={}
queue_row = []
for content in file_content:
         queue_counters={}
         if 'aging' in content:
                 # aging 0 100
                 columns = ', '.join(map(str, 
content[:1])).replace('-','_').lower()
                 print "columns:", columns
                 total_values =''.join(map(str, content[1:2]))
                 aging_values = '\t'.join(map(str, content[2:]))

                 aging_dict['total'], aging_dict[columns] = 
total_values, aging_values
                 queue_counters[columns] = aging_dict
         if 'highway' in content:
                 #highway        |       4       |       disable |       25
                 columns = ''.join(map(str, 
content[:1])).replace('-','_').lower()
                 lanes_values  =''.join(map(str, content[1:2]))
                 state_values = ''.join(map(str, content[2:3])).strip('')
                 limit_values = ''.join(map(str, content[3:4])).strip('')

                 highway_dict['lanes'], highway_dict['state'], 
highway_dict['limit(mph)'] = lanes_values, state_values, limit_values
                 queue_counters[columns] = highway_dict
         queue_row.append(queue_counters)


print
print "h dict:", highway_dict
print
print "aging dict:", aging_dict
print
print "q counters:", queue_counters
for key, item in  queue_counters.iteritems():
     print key, item

print
print "q row:", queue_row
for item in queue_row:
     print item






-- 
DaveA



More information about the Python-list mailing list