[Tutor] Summing across a row

Kent Johnson kent_johnson at skillsoft.com
Wed Oct 6 04:10:35 CEST 2004


 >>> line = '1, 2, 3, 4'  # original input
 >>> line.split(', ')  # split it up into fields
['1', '2', '3', '4']

 >>> map(int, line.split(', '))  # convert the field values to integers
[1, 2, 3, 4]

 >>> [int(x) for x in line.split(', ')]  # another way to convert to integers
[1, 2, 3, 4]

 >>> sum(map(int, line.split(', ')))  # Add them all up
10

Kent

At 02:22 PM 10/5/2004 -0600, Michael Knowles wrote:
>I've been successful in reading in a comma-delimited text file and summing
>the columns but I can't figure out how to sum across a row.
>
>Ex. text file:
>1, 2, 3, 4 =====> ?? Haven't figured this out
>5, 6, 7, 8 =====> ??
>9, 10, 11, 12 ==> ??
>=========
>15, 18, 21, 24 ===> I 've figured this out
>
>Thanks,
>Mike
>
>
>*******************************************
>Mike Knowles
>Information Systems Analyst
>SI International
>USDA, Forest Service, Rocky Mountain Research Station
>2150 Centre Ave, Bldg A, Suite 361
>Ft. Collins,  CO  80526
>Phone:  (970) 295-5979
>Fax:  (970) 295-5959
>e-mail:  mknowles at fs.fed.us
>*******************************************
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list