Try this:

     import string

     lines = ["1/12/2001,3,6,8,34,2", "1/13/2001,4,7,9,35,3"]
     numbers_array = {}

     for line in lines:
         data = string.split(line, ",")
         date = data[0]
         numbers_array[date] = data[1:]  # This is a slice of the list


Now you can access your information by:

     print numbers_array["1/12/2001"]   # prints the list
     print numbers_array["1/12/2001"][0]  # in your example, prints 3

Hope this helps!

Howard
http://howard.editthispage.com


At 11:06 AM 4/9/2001 -0700, David Ransier wrote:
>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