0

I would like to change my data-structure that get from my data-files in such a way that I get a list of all coordinate-values for every coordinates (so a list for all coordinates filled with values)

e.g. for i in range (files):

open file

file_output = [[0,4,6],[9,4,1],[2,5,3]]

second loop

file_output = [[6,1,8],[4,7,3],[3,7,0]]

to

coordinates = [[0,6],[4,1],[6,8],[9,4],[4,7],[1,3],[2,3],[5,7],[3,0]]

It should be noted that I use over 1000 files of this format, which I should merge.

Community
  • 1
  • 1
Wesley
  • 23
  • 4

3 Answers3

1

You could also explore the built-in zip() function

>>> l = []
>>> for k,v in zip(a,b):
        l.append(zip(k,v))
>>> print l
[[0,6],[4,1],[6,8],[9,4],[4,7],[1,3],[2,3],[5,7],[3,0]]
rafaelc
  • 57,686
  • 15
  • 58
  • 82
0
>>> a = [[0,4,6],[9,4,1],[2,5,3]]
>>> b = [[6,1,8],[4,7,3],[3,7,0]]
>>> from itertools import chain
>>> zip(chain(*a),chain(*b))
[(0, 6), (4, 1), (6, 8), (9, 4), (4, 7), (1, 3), (2, 3), (5, 7), (3, 0)]
>>> 
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
0

This should be useful.

[zip(i,j) for i in a for j in b]

However it provides list of tuples, which should satisfy your needs.

If there will only be two lists, you can use this as well.

[[i, j] for i in a for j in b]
Max Paython
  • 1,595
  • 2
  • 13
  • 25