0

I have several files where the first column is text and the rest numbers like

first.txt

A 1 3 5 7 B 9 11 13 15

second.txt

A 0 4 6 8
B 10 12 14 16

which I import like

a=[] 
b=[]
descr=[]

descr.append( np.genfromtxt('first_file.txt', dtype=None,usecols=(0)))
for counter in range(1,5) :
    a.append( np.genfromtxt('first_file.txt', dtype=None,usecols=(counter+1)))
    b.append( np.genfromtxt('second_file.txt', dtype=None,usecols(counter+1)))

now basically, descr[] hold the string of the first column while a and b are arrays which I need now to sum up per column and print something like

summed result

A 1 7 11 15

B 19 23 27 31

I ve tried this

total = a+b

lines = ['  \t'.join([str(x[i]) if len(x) > i else ' ' for x in total]) for i in range(len(max(total)))]
for i in range(len(descr)):
    if descr[i] != '' :
        print '{}  {}'.format(descr[i], lines[i])

but I get

lines = [' \t'.join([str(x[i]) if len(x) > i else ' ' for x in tot]) for i in range(len(max(tot)))] ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Tarifazo
  • 4,118
  • 1
  • 9
  • 22
Alex
  • 191
  • 1
  • 9
  • Print the values of `a`, `b`, `descr` after you load them. It doesn't matter how you loaded those arrays: that's not where end error is happening. – Mad Physicist Nov 12 '19 at 12:30
  • the print does not reveal anything wrong... I can see the arrays filled ok – Alex Nov 12 '19 at 12:33
  • can you print (and add here) the first 5 or 10 lines of `total`? – Tarifazo Nov 12 '19 at 12:36
  • @Mstaino in principle the files are not so small like in the example I gave - They tot looks more like ``[array([8.7213804e+05, 8.1190010e+04, 2.1497340e+04, 2.1497340e+04, 1.9885670e+04, 1.3772000e+02, 1.3772000e+02]), array([8.7213804e+05, 8.1190010e+04, 2.1497340e+04, 2.1497340e+04, 1.9885670e+04, 5.4840000e+01, 5.4840000e+01])...]`` – Alex Nov 12 '19 at 12:45
  • I think you're missing the point. I expect that your load was totally fine. It's completely irrelevant to your question. You should edit your question to have a minimal reproducible example. We don't need to see your fill arrays, or the text files, just a small sample array and the operation that fails – Mad Physicist Nov 12 '19 at 13:20
  • I added an answer with a correction to the expression that is giving the error, however I do not know if this reproduces what you expect as output. As a general rule, add a small sample of the array and a small sample of the output to the question so we can better understand what you are trying to achieve. – Tarifazo Nov 12 '19 at 13:46

2 Answers2

1

I could not understand your question completely and your comment with total also did not help me much. Considering your total issue, you can do simply:

import numpy as np 
a = []
b = []

descr = np.genfromtxt('first.txt', dtype=None, usecols=(0))

for counter in range(1,5):
    temp = np.genfromtxt('first.txt', dtype=None,usecols=(counter))
    a.append(temp)
    temp = np.genfromtxt('second.txt', dtype=None,usecols=(counter))
    b.append(temp)

total = []
seq = []
seq.append(a)
seq.append(b)
total.append(np.sum(seq,axis=0))
print("Total: ")
print(total)

print("\nResult: ")
for i in range(len(descr)):
    if descr[i] != '' :
        print (descr[i], total[0][:,i])

It gives you the following result:

Total: 
[array([[ 1, 19],
       [ 7, 23],
       [11, 27],
       [15, 31]])]

Result: 
b'A' [ 1  7 11 15]
b'B' [19 23 27 31]
halfer
  • 19,824
  • 17
  • 99
  • 186
SSharma
  • 951
  • 6
  • 15
0

One of your errors is in the expression max(total), assuming you want the maximum length of an array use:

l = max(map(len, total))

Then, following your syntax:

lines = ['  \t'.join([str(x[i]) if len(x) > i else ' ' for x in total]) for i in range(l)]

If this does not give the expected result, please add a sample of your expected output

Tarifazo
  • 4,118
  • 1
  • 9
  • 22