Search code examples
pythonlistloopsfor-looppretty-print

Loop print of 2 columns in to 1


Suppose I have an array of 2 columns. It looks like this column1 = [1,2,3,...,830] column2 = [a,b,c,...]

I want to have output in a print form of single columns, that includes value of both columns one by one. output form: column = [1,a,2,b ....]

I tried to do by this code,

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

Solution

  • I do not understand your code completely, is the reference to numpy really relevant for your question? What is M?

    If you have two lists of the same lengths you can get pairs of elements using the zip builtin.

    A = [1, 2, 3]
    B = ['a', 'b', 'c']
    
    for a, b in zip(A, B):
        print(a)
        print(b)
    

    This will print

    1
    a
    2
    b
    3
    c