Try this:

numbers_ARRAY_X_date = {}
for line in lines:
     data = string.split(line,',')
     numbers_ARRAY_X_date[data[0]] = data[1:]

This will give you a dictionary item searchable by your date.  Watch out
for duplicate dates unless you expect it to overwrite your dictionary item
values.

If you want it to be a list do it like this

numbers_ARRAY_X_date = []
for line in lines:
     data = string.split(line,',')
     numbers_ARRAY_X_date.append({data[0]:  data[1:]})

This way you will have a list of dictionaries, and duplicate dates will not
get overwritten.

-Ryan



                                                                                       
                               
                    David Ransier                                                      
                               
                    <[EMAIL PROTECTED]>           To:     
"Python-ActiveState (E-mail)"                    
                    Sent by:                                 
<[EMAIL PROTECTED]>                  
                    [EMAIL PROTECTED]        cc:                       
                               
                    eState.com                               Subject:     Help: 
dictionary of lists                   
                                                                                       
                               
                                                                                       
                               
                    04/09/01 02:06 PM                                                  
                               
                                                                                       
                               
                                                                                       
                               




Hello experts. I'm very experienced with Perl, but new to Python. I'm
trying
to learn Python by converting some of my Perl scripts.

I have a program that reads a file where each line is a date followed by
some numbers. Like this:
     1/12/2001,3,6,8,34,2

This piece of code seems to work ok to organize the numbers as a list in a
dictionary organized by data.

     for line in lines:
          data = string.split(line,',')
          date = data[0]
          numbers_ARRAY_X_date[date] =
[data[1],data[2],data[3],data[4],data[5]]

However, I would like to have a general solution that could add an
arbitrary
number of items to the dictionary list. I tried this, but the code fails:

     for line in lines:
          data = string.split(line,',')
          date = data[0]
          for n in range(1,6):
               numbers_ARRAY_X_date[date].append(data[n])

The error message complains that append  is not appropriate for the data
type: numbers_ARRAY_X_date[date]

Is there a way to coerce numbers_ARRAY_X_date[date] into a list? or is
there
another way to achieve this?


Thanks,
David R
____________________________________________
David Ransier
[EMAIL PROTECTED]
Office:   503-416-3812                 111 SW 5th Ave.
Fax:      503-416-3801                 Suite 2200
Mobile:  360-921-5584                 Portland, OR 97204

_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activepython



_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activepython

Reply via email to