473,394 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Like a matrix

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.

Jul 18 '05 #1
3 2407
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 && ma*@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
Jul 18 '05 #2
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:
m, n, o = zip(u, v)
m (1, 3) n (2, 2) avg(o) 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

Jul 18 '05 #3
Peter Otten wrote:
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:

m, n, o = zip(u, v)
m
(1, 3)
n
(2, 2)
avg(o)
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.
map(avg, zip(u,v))

[2.0, 2.0, 2.0]
Ryan

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
13
by: Charulatha Kalluri | last post by:
Hi, I'm implementing a Matrix class, as part of a project. This is the interface I've designed: class Matrix( )
6
by: memocan | last post by:
#include <iostream> using namespace std; int x; //global variable matrix int main() { x= new float ; //initialize the size now }
14
by: amitnanda | last post by:
Hi Guys, I have a matrix multiplication program in C that multiplies two matrices. When their size is 3*3 or 800*800, the program runs fine. But above that size, I get a "segmentation fault"....
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
2
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
18
by: Hypnotik | last post by:
Hello everyone. I'm writing a program which uses a class called matrix. I have written all of the different functions, constructor, etc. When I run the program I receive "Constructor", which I...
2
by: devnew | last post by:
hi i am looking for some info about mapping btw values in an array and corresponding columns of a matrix i have an numpy array= and a numpy matrix object= matrix((, , , ))
2
by: rijaalu | last post by:
I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error. include <iostream> using namespace std; class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.