Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45060

Re: help on Implementing a list of dicts with no data pattern

Date 2013-05-09 18:32 -0400
From Dave Angel <davea@davea.name>
Subject Re: help on Implementing a list of dicts with no data pattern
References <c27c3eef-44f5-467c-b3a3-119263deb035@googlegroups.com> <5bc1d8b8-61b1-446d-8487-1e72c1ddf925@googlegroups.com> <mailman.1500.1368123592.3114.python-list@python.org> <3bcb3a3a-f1e2-4e31-b917-6a4391ddb9fa@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1508.1368138770.3114.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

help on Implementing a list of dicts with no data pattern rlelis <ricardo.lelis3@gmail.com> - 2013-05-08 16:47 -0700
  Re: help on Implementing a list of dicts with no data pattern Dave Angel <davea@davea.name> - 2013-05-08 20:05 -0400
  Re: help on Implementing a list of dicts with no data pattern MRAB <python@mrabarnett.plus.com> - 2013-05-09 01:16 +0100
  Re: help on Implementing a list of dicts with no data pattern rlelis <ricardo.lelis3@gmail.com> - 2013-05-09 02:57 -0700
    Re: help on Implementing a list of dicts with no data pattern Dave Angel <davea@davea.name> - 2013-05-09 08:26 -0400
    Re: help on Implementing a list of dicts with no data pattern Neil Cerutti <neilc@norwich.edu> - 2013-05-09 13:08 +0000
  Re: help on Implementing a list of dicts with no data pattern rlelis <ricardo.lelis3@gmail.com> - 2013-05-09 07:33 -0700
    Re: help on Implementing a list of dicts with no data pattern Dave Angel <davea@davea.name> - 2013-05-09 10:50 -0400
  Re: help on Implementing a list of dicts with no data pattern rlelis <ricardo.lelis3@gmail.com> - 2013-05-09 09:14 -0700
    Re: help on Implementing a list of dicts with no data pattern Dave Angel <davea@davea.name> - 2013-05-09 14:19 -0400
      Re: help on Implementing a list of dicts with no data pattern rlelis <ricardo.lelis3@gmail.com> - 2013-05-09 14:22 -0700
        Re: help on Implementing a list of dicts with no data pattern Dave Angel <davea@davea.name> - 2013-05-09 18:32 -0400
          Re: help on Implementing a list of dicts with no data pattern Neil Cerutti <neilc@norwich.edu> - 2013-05-10 12:54 +0000

csiph-web