Like a matrix

Y

Yazar Yolait

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.
I was thinking of a dictionary but how do you add entries to a dictionary,
you can't can you? Like I would add 0 to a dictionary that holds the 0th
column. And then when I get to line 2, I would add 4 to the dictionary
which has the 0th column data.
 
E

Erik Max Francis

Yazar said:
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}}
 
P

Peter Otten

Yazar said:
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.
I was thinking of a dictionary but how do you add entries to a dictionary,
you can't can you? Like I would add 0 to a dictionary that holds the 0th
column. And then when I get to line 2, I would add 4 to the dictionary
which has the 0th column data.
from __future__ import division
def avg(alist): return sum(alist)/len(alist) ....
u = [1,2,3]
v = [3,2,1]
avg(u)
2.0

So that was the easy part. Now let's create the columns:

So that wasn't particular hard, now you see it :)
zip() returns tuples instead of lists, but as you see above that does no
harm.

Peter
 
R

ryan scott

Peter said:
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.
I was thinking of a dictionary but how do you add entries to a dictionary,
you can't can you? Like I would add 0 to a dictionary that holds the 0th
column. And then when I get to line 2, I would add 4 to the dictionary
which has the 0th column data.
from __future__ import division
def avg(alist): return sum(alist)/len(alist)
...
u = [1,2,3]
v = [3,2,1]
avg(u)
2.0

So that was the easy part. Now let's create the columns:

(1, 3)
(2, 2)
2.0


So that wasn't particular hard, now you see it :)
zip() returns tuples instead of lists, but as you see above that does no
harm.

Peter

As an addition to this post, you could use avg in map to return a list
of your averages.
[2.0, 2.0, 2.0]


Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top