2013-10-17 114 views
0

假设我有一个2列的数组。它看起来像这样 column1 = [1,2,3,...,830] column2 = [a,b,c,...]2列循环打印到1

我想要在单列,其中包括两列的价值。输出形式:柱= [1,,2,B ....]

我试图通过该代码来执行,

dat0 = np.genfromtxt("\", delimiter = ',') 
mu = dat0[:,0] 
A = dat0[:,1] 
print(mu,A) 
R = np.arange(0,829,1) 
l = len(mu) 
K = np.zeros((l, 1)) 
txtfile = open("output_all.txt",'w') 
for x in mu: 
    i = 0 
    K[i,0] = x 
    dat0[i,1] = M 
    txtfile.write(str(x)) 
    txtfile.write('\n') 
    txtfile.write(str(M)) 
    txtfile.write('\n') 
print K 

回答

1

我不完全理解您的代码,是numpy的基准真的与你的问题有关?什么是M

如果您有两个相同长度的列表,您可以使用zip内建的元素获取元素对。

A = [1, 2, 3] 
B = ['a', 'b', 'c'] 

for a, b in zip(A, B): 
    print(a) 
    print(b) 

这将打印

1 
a 
2 
b 
3 
c 
+0

我觉得OP想用那些元素的列表。因此,我猜想附加是为了? – Jblasco

+0

追加可能是最清晰的,如果你更喜欢只使用迭代,你可以使用'[x为zip中的对(A,B)为x] [ – Leonhard

+0

聪明,我试图这样做,但没有'用一种干净的方式管理:)。 – Jblasco

0

我敢肯定有更好的方式来做到这一点,但一个方法是

>>> a = numpy.array([[1,2,3], ['a','b','c'],['d','e','f']]) 
>>> new_a = [] 
>>> for column in range(0,a.shape[1]):   # a.shape[1] is the number of columns in a 
...  for row in range(0,a.shape[1]):   # a.shape[0] is the number of rows in a 
...    new_a.append(a[row][column]) 
... 
>>> numpy.array(new_a) 
array(['1', 'a', 'd', '2', 'b', 'e', '3', 'c', 'f'], 
    dtype='|S1')