Like a matrix

Erik Max Francis max at alcyone.com
Sun Oct 26 01:47:09 EST 2003


Yazar Yolait wrote:

> I have a string of numbers in a file like so:
> 0 3 23.3 352 45 4
> 4 45 23 54 4 5.4 .6
> 
> I need to average them horizontally and vertically.  Horizontally is
> easy
> since I'm reading one line at a time, but how can I do it vertically
> like 0
> and 4, 3 and 45, etc.

Why not read it into a list of lists, and then iterate both ways?

> I was thinking of a dictionary but how do you add entries to a
> dictionary,
> you can't can you?

Sure you can:

>>> d = {}
>>> d[0] = 'zero'
>>> d
{0: 'zero'}
>>> d[1] = 'one'
>>> d
{0: 'zero', 1: 'one'}
>>> d[10] = {'a': 1, 'b': 2, 'c': 3}
>>> d
{0: 'zero', 1: 'one', 10: {'a': 1, 'c': 3, 'b': 2}}

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ The price of eternal vigilance is indifference.
\__/  Marshall McLuhan




More information about the Python-list mailing list