0

I have 3 files (file1.txt, file2.txt, file3.txt) that look like:

file1:

-7  12
-6  13
-5  11
-4  10
-3  4
-2  8
-1  7
0   9
1   10

file2:

-5  13
-4  4
-3  5
-2  4
-1  7
0   6
1   9
2   10
3   2

file3:

-2  3
-1  14
0   8
1   7
2   3
3   9
4   10
5   8
6   3

I want to align the zeros of the arrays I read from these files and get the sum of the values on the right, that is getting something like:

-7  ->  12  + 0  + 0  =  12
-6  ->  13  + 0  + 0  =  13
-5  ->  11  + 13 + 0  =  24
-4  ->  10  + 4  + 0  =  14
-3  ->  4   + 5  + 0  =  9
-2  ->  8   + 4  + 3  =  15
-1  ->  7   + 7  + 14 =  28
0   ->  9   + 6  + 8  =  23
1   ->  10  + 9  + 7  =  26
2   ->  0   + 10 + 3  =  13
3   ->  0   + 2  + 9  =  11
4   ->  0   + 0  + 10 =  10
5   ->  0   + 0  + 8  =  8
6   ->  0   + 0  + 3  =  3
7   ->  0   + 0  + 0  =  0

How could I do that, with Python? Thank you in advance.

4
  • have you tried anything? Commented Apr 13, 2016 at 20:55
  • I feel a bit ashamed, but I've got no ideas... Commented Apr 13, 2016 at 21:01
  • Do you know how to read values from a file or are you stuck on that? Commented Apr 13, 2016 at 21:03
  • You can read each file to a dictionary with key = first column and value = second column. That should get you started.. Commented Apr 13, 2016 at 21:03

2 Answers 2

1

Just an idea but there would be a lot them:

  1. import all those pairs into one list of pairs
  2. create a second empty list of pairs
  3. parse every pair of the first list and check if the first element of the pair is somewhere among the first elements of the pairs of the second list. If it already is then sum the second element of the pairs, otherwise just append the pair to the second list.
Sign up to request clarification or add additional context in comments.

Comments

0

From start to finish, assuming the intended output is csv, as well as the input files are csv delimited via ",". Output methods are very flexible however if something else was anticipated.

import petl

file1 = petl.fromcsv('file1.csv')
file1 = petl.pushheader(file1, ['position', 'value1'])
file2 = petl.fromcsv('file2.csv')
file2 = petl.pushheader(file2, ['position', 'value2'])
file3 = petl.fromcsv('file3.csv')
file3 = petl.pushheader(file3, ['position', 'value3'])


file1_joined_file2 = petl.outerjoin(file1, file2, key='position')
all_joined = petl.outerjoin(file1_joined_file2, file3, key='position')
all_joined = petl.convert(all_joined, 'value1', {None: 0})
all_joined = petl.convert(all_joined, 'value2', {None: 0})
all_joined = petl.convert(all_joined, 'value3', {None: 0})
all_joined = petl.convert(all_joined, ('position', 'value1', 'value2', 'value3'), int)
all_joined = petl.addfield(all_joined, 'sum', lambda rec: rec['value1'] + rec['value2'] + rec['value3'])
all_joined = petl.sort(all_joined, 'position')

print petl.lookall(all_joined)
petl.tocsv(all_joined, 'done.csv')

Output from the print above:

+----------+--------+--------+--------+-----+
| position | value1 | value2 | value3 | sum |
+==========+========+========+========+=====+
|       -7 |     12 |      0 |      0 |  12 |
+----------+--------+--------+--------+-----+
|       -6 |     13 |      0 |      0 |  13 |
+----------+--------+--------+--------+-----+
|       -5 |     11 |     13 |      0 |  24 |
+----------+--------+--------+--------+-----+
|       -4 |     10 |      4 |      0 |  14 |
+----------+--------+--------+--------+-----+
|       -3 |      4 |      5 |      0 |   9 |
+----------+--------+--------+--------+-----+
|       -2 |      8 |      4 |      3 |  15 |
+----------+--------+--------+--------+-----+
|       -1 |      7 |      7 |     14 |  28 |
+----------+--------+--------+--------+-----+
|        0 |      9 |      6 |      8 |  23 |
+----------+--------+--------+--------+-----+
|        1 |     10 |      9 |      7 |  26 |
+----------+--------+--------+--------+-----+
|        2 |      0 |     10 |      3 |  13 |
+----------+--------+--------+--------+-----+
|        3 |      0 |      2 |      9 |  11 |
+----------+--------+--------+--------+-----+
|        4 |      0 |      0 |     10 |  10 |
+----------+--------+--------+--------+-----+
|        5 |      0 |      0 |      8 |   8 |
+----------+--------+--------+--------+-----+
|        6 |      0 |      0 |      3 |   3 |
+----------+--------+--------+--------+-----+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.