transpose array

Y

yoshco

hello everyone
i have 3 arrays
xVec=[a1,a2,a3,a4,a5]
yVec=[b1.b2.b3.b4.b5]
zVec=[c1,c2,c3,c4,c5]

and i want to output them to a ascii file like so

a1,b1,c1
a2,b2,c2
a3,b3,c3
....

now i'm using

print >>f, str(xVec).replace('[',' ').replace(']', ' ')
print >>f, str(yVec).replace('[',' ').replace(']', ' ')
print >>f, str(zVec).replace('[',' ').replace(']', ' ')

which dumps them like

a1,a2,a3,a4,a5
b1.b2.b3.b4.b5
c1,c2,c3,c4,c5
 
P

Peter Otten

yoshco said:
hello everyone
i have 3 arrays
xVec=[a1,a2,a3,a4,a5]
yVec=[b1.b2.b3.b4.b5]
zVec=[c1,c2,c3,c4,c5]

and i want to output them to a ascii file like so

a1,b1,c1
a2,b2,c2
a3,b3,c3
...

now i'm using

print >>f, str(xVec).replace('[',' ').replace(']', ' ')
print >>f, str(yVec).replace('[',' ').replace(']', ' ')
print >>f, str(zVec).replace('[',' ').replace(']', ' ')

which dumps them like

a1,a2,a3,a4,a5
b1.b2.b3.b4.b5
c1,c2,c3,c4,c5
xVec=[a1,a2,a3,a4,a5]
yVec=[b1,b2,b3,b4,b5]
zVec=[c1,c2,c3,c4,c5]
import sys, csv
from itertools import izip
csv.writer(sys.stdout).writerows(izip(xVec, yVec, zVec))
a1,b1,c1
a2,b2,c2
a3,b3,c3
a4,b4,c4
a5,b5,c5

Peter
 
E

Edward A. Falk

hello everyone
i have 3 arrays
xVec=[a1,a2,a3,a4,a5]
yVec=[b1.b2.b3.b4.b5]
zVec=[c1,c2,c3,c4,c5]

and i want to output them to a ascii file like so

a1,b1,c1
a2,b2,c2
a3,b3,c3
...

Elegant or obfuscated, you be the judge:

vv = [xVec, yVec, zVec]

for i in range(len(xVec)):
print >>f, ", ".join([x for x in vv])


To be honest, I'd be more likely to do it like this, for readability if
for no other reason:

for i in range(len(xVec)):
print >>f, "%f, %f, %f" % (xVec, yVec, zVec)



It might help if we knew what you're *really* trying to do.
 
I

Ishwor Gurung

Hi,
xVec=[a1,a2,a3,a4,a5]
yVec=[b1.b2.b3.b4.b5]
zVec=[c1,c2,c3,c4,c5]

and i want to output them to a ascii file like so

a1,b1,c1
a2,b2,c2
a3,b3,c3
...

now i'm using

   print >>f, str(xVec).replace('[',' ').replace(']', ' ')
   print >>f, str(yVec).replace('[',' ').replace(']', ' ')
   print >>f, str(zVec).replace('[',' ').replace(']', ' ')

which dumps them like

 a1,a2,a3,a4,a5
 b1.b2.b3.b4.b5
 c1,c2,c3,c4,c5
xVec=[a1,a2,a3,a4,a5]
yVec=[b1,b2,b3,b4,b5]
zVec=[c1,c2,c3,c4,c5]
import sys, csv
from itertools import izip
csv.writer(sys.stdout).writerows(izip(xVec, yVec, zVec))
a1,b1,c1
a2,b2,c2
a3,b3,c3
a4,b4,c4
a5,b5,c5

Or, http://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html :)
 
S

Steven D'Aprano

hello everyone
i have 3 arrays
xVec=[a1,a2,a3,a4,a5]
yVec=[b1.b2.b3.b4.b5]
zVec=[c1,c2,c3,c4,c5]

and i want to output them to a ascii file like so

a1,b1,c1
a2,b2,c2
a3,b3,c3
...


f = open('myfile.txt', 'w')
for t in zip(xVec, yVec, zVec):
f.write('%s, %s, %s\n' % t)
f.close()


If the lists are really huge, you should use itertools.izip() instead of
zip().
 
A

alex23

hello everyone
i have 3 arrays
xVec=[a1,a2,a3,a4,a5]
yVec=[b1.b2.b3.b4.b5]
zVec=[c1,c2,c3,c4,c5]

and i want to output them to a ascii file like so

a1,b1,c1
a2,b2,c2
a3,b3,c3
...

I'd probably go with something like the following:

all_arrays = zip(xVec, yVec, zVec)
formatter = lambda t: '%s\n' % ', '.join(map(str, t))
with open('transpose.txt', 'w') as out:
out.writelines(formatter(t) for t in all_arrays)

Hope this helps.
 
Y

yoshco

hello everyone
i have 3 arrays
xVec=[a1,a2,a3,a4,a5]
yVec=[b1.b2.b3.b4.b5]
zVec=[c1,c2,c3,c4,c5]
and i want to output them to a ascii file like so
a1,b1,c1
a2,b2,c2
a3,b3,c3
...

I'd probably go with something like the following:

all_arrays = zip(xVec, yVec, zVec)
formatter = lambda t: '%s\n' % ', '.join(map(str, t))
with open('transpose.txt', 'w') as out:
    out.writelines(formatter(t) for t in all_arrays)

Hope this helps.

thanks to you all
 
A

Aahz

Surely more Pythonic would be:

for t in zip(xVec, yVec, zVec):
print >>f, ", ".join(t)

Except that you *really* want itertools.izip() if these vectors are
likely to be any significant size.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"You could make Eskimos emigrate to the Sahara by vigorously arguing --
at hundreds of screens' length -- for the wonder, beauty, and utility of
snow." --PNH to rb in r.a.sf.f
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top