Good afternoon,
I have some data that looks like this:
data = [[1, 2], [3, 4], [5, 6]]
I want to send columns 1 and 2 to a function
as two variables, say "plot(col1,col2 )".
I can get a row by data[r], but how do I get a
column? data[:][c] would have been my guess,
but that returns the same list as data[r].
I can solve this with two additional lists and
a for loop, but that seems like an ugly hack.
Five additional lines just seems clumsy. :-)
Is there a more elegant solution than this?
col1 = []
col2 = []
for i in range(len(data) ):
col1 += [a[i][0]]
col2 += [a[i][1]]
/Arvid Andersson 6 12858
Avid Andersson wrote: Good afternoon,
I have some data that looks like this: data = [[1, 2], [3, 4], [5, 6]]
I want to send columns 1 and 2 to a function as two variables, say "plot(col1,col2 )".
I can get a row by data[r], but how do I get a column? data[:][c] would have been my guess, but that returns the same list as data[r].
I can solve this with two additional lists and a for loop, but that seems like an ugly hack. Five additional lines just seems clumsy. :-)
Is there a more elegant solution than this?
col1 = [] col2 = [] for i in range(len(data) ): col1 += [a[i][0]] col2 += [a[i][1]]
/Arvid Andersson
Try something like:
coldata=[[x[0] for x in data], [x[1] for x in data]]
plot(*coldata)
Larry Bates
Syscon, Inc.
On Thu, 21 Oct 2004 17:19:04 +0200, Arvid Andersson <ar***@linux.se > wrote: I have some data that looks like this: data = [[1, 2], [3, 4], [5, 6]]
I want to send columns 1 and 2 to a function as two variables, say "plot(col1,col2 )".
From a interpreter session : data = [[1, 2], [3, 4], [5, 6]] col1 = [ pt[0] for pt in data ] col1
[1, 3, 5]
import Numeric as N N.array(data)
array([[1, 2],
[3, 4],
[5, 6]]) N.array(data, N.Float)
array([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
pts = N.array(data, N.Float) pts[:,0]
array([ 1., 3., 5.])
Hope that helps. Don't hesitate to try directly at the interpreter
prompt.
Arvid Andersson <ar***@linux.se > wrote: Good afternoon,
I have some data that looks like this: data = [[1, 2], [3, 4], [5, 6]]
I want to send columns 1 and 2 to a function as two variables, say "plot(col1,col2 )".
I can get a row by data[r], but how do I get a column? data[:][c] would have been my guess, but that returns the same list as data[r].
Right: data[:] makes a shallow copy of data, then you index into that --
no use.
col_c = [row[c] for row in data]
is probably the simplest, best and most idiomatic way to extract column
'c' from this kind of list-of-lists.
Is there a more elegant solution than this?
col1 = [] col2 = [] for i in range(len(data) ): col1 += [a[i][0]] col2 += [a[i][1]]
Several -- this one has not a few small imperfections. For example,
there is generally no need to iterate on indices:
for row in data:
col1 += [row[0]]
&c, would already be a small enhancement. Moreover,
'col1 += [something]' is just a complicated way to express
col1.append(som ething), so you could further move to:
for row in data:
col1.append(row[0])
&c. And finally, you can recognize the pattern that:
anylist = []
for item in somewhere:
anylist.append( <expression using item>)
is exactly what's meant by a list comprehension:
anylist = [<expression using item> for item in somewhere]
thus getting to the above-suggested
col1 = [row[0] for row in data]
and the like. Since a list comprehension is an expression, you don't
have to give it a name if you don't want to; just pass it as the
argument in the function call. Moreover, if you do this thing often,
much legibility at the cost of a tiny overhead could be had by having
def col(data, colindex):
return [row[colindex] for row in data]
and using col(data, 0) and col(data, 1) as arguments to the function
you're calling.
Alex
"F. Petitjean" <li***********@ noos.fr> wrote in message
news:41******** *************@n ews.free.fr... On Thu, 21 Oct 2004 17:19:04 +0200, Arvid Andersson <ar***@linux.se >
wrote: I have some data that looks like this: data = [[1, 2], [3, 4], [5, 6]]
I want to send columns 1 and 2 to a function as two variables, say "plot(col1,col2 )".
.... import Numeric as N
....
Hope that helps. Don't hesitate to try directly at the interpreter prompt.
Ok, let's see what happens:
import Numeric as n
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named Numeric
I think you left out a step in your instructions... . ;-)
--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Arvid Andersson wrote: I have some data that looks like this: data = [[1, 2], [3, 4], [5, 6]]
I want to send columns 1 and 2 to a function as two variables, say "plot(col1,col2 )".
In the general case list comprehensions are the way to go, but for the
problem specified above you can use a neat zip() trick, assuming that
plot() also accepts tuples instead of lists: def plot(col1, col2): # plots nothing, but shows its arguments
.... print "col1", col1
.... print "col2", col2
.... data = [[1, 2], [3, 4], [5, 6]] plot(*zip(*data ))
col1 (1, 3, 5)
col2 (2, 4, 6)
The star prefix feeds the items in the following list as arguments to the
function, so zip(*[[1, 2], [3, 4]]) is the same as zip([1, 2], [3, 4]),
which in turn gives you [(1, 3), (2, 4)] as the result.
The same technique can then be repeated with plot().
Peter
On Thu, 21 Oct 2004 12:10:50 -0400, Russell Blau <ru******@hotma il.com> wrote: "F. Petitjean" <li***********@ noos.fr> wrote in message news:41******** *************@n ews.free.fr... On Thu, 21 Oct 2004 17:19:04 +0200, Arvid Andersson <ar***@linux.se > wrote: > > I have some data that looks like this: > data = [[1, 2], [3, 4], [5, 6]] > ... >>> import Numeric as N ...
Hope that helps. Don't hesitate to try directly at the interpreter prompt.
Ok, let's see what happens: import Numeric as n
Traceback (most recent call last): File "<stdin>", line 1, in ? ImportError: No module named Numeric
I think you left out a step in your instructions... . ;-)
Numeric is the name of the "Numeric python" package aka Numpy.
I think that you can find it on sourceforge.net , alongside a new version
called Numarray. So, The numeric arrays are interesting as they store
homogeneous items (typically integers or floats) and can have multiple
dimensions. Slicing (pts[:,1] for instance) is very convenient.
Regards. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: sangeetha |
last post by:
Hi,
i need to transpose a nx1 matrix to 1xn matrix inorder to multiply
with nxn matrix in c language ...
can anyone help in this coding ..the nx1 matrix is pi the data type
is double *pi..this is used through out the program ..now i need this
transpose for further calculations....
Kindly help as soon as possible.....if this is not possible kindly
suggest me something...
|
by: Peterwkc |
last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back.
By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this?
Second Program:
/* Best Method to allocate memory for 2D Array because it's
...
|
by: adam.kleinbaum |
last post by:
Hi there,
I'm a novice C programmer working with a series of large (30,000 x
30,000) sparse matrices on a Linux system using the GCC compiler. To
represent and store these matrices, I'd like to implement the sparse
matrices as a doubly-linked list, in which each non-zero cell is
stored roughly as follows:
int rownum
int colnum
|
by: devnew |
last post by:
hi
i am looking for an efficient way to get a specific column of a
numpy.matrix ..
also i want to set a column of the matrix with a given set of
values ..i couldn't find any methods for this in matrix doc..do i have
to write the functions from scratch?
TIA
dn
|
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((,
,
,
))
| |
by: cLoque |
last post by:
Hi,
I would like to know how to create a dictionary based matrix. Where column and row are considered in placing my desired value. and will pass value as dictionary
just like this
def __init__(self, mtx={}, m=0, n=0):
|
by: atemuoh1991 |
last post by:
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
struct SMatrix
{
double** pValues;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |