1

I am reading in a text file containing several lists with each on its own line. The elements in a given list are non-integer x,y co-ordinates that need to be placed into an array such that:

[x1, y1, x2, y2, x3, y3,....xn, yn]

becomes:

x1 x2 x3....xn

y1 y2 y3....yn

but in the same 2d array.

I'm very new to coding and have tried various methods but to no avail. Right now I am looping over the number of co-ordinate pairs and trying to append the values within the loop but am unsure how to do this.

This is how I have started it:

with open('textfile.txt', 'r') as infile:
    for line in infile:
        array=[]
        number=(line.count(" ")+1)/2 #number of x,y pairs in list
        for i in range(0, number-1):
            

Now I don't know where to go from here so any help would be greatly appreciated! Thanks

Community
  • 1
  • 1
Jsn4
  • 11
  • 1
  • 3

5 Answers5

1

Since you are new to python and you are dealing with numerical data, you may want to consider the de facto standard module for such manipulations, numpy. With it, your problem becomes trivial:

import numpy as np

A = [1,2,3,4,5,6,7,8]
print np.array(A).reshape((len(A)/2,2)).T

>>> [[1 3 5 7]
     [2 4 6 8]]

Here we cast the list in to a numpy array np.array, reshape it into the size we desire and take the transpose T to get from (x1,y1),(x2,y2),... to (x1,x2,...),(y1,y2,...).

Hooked
  • 84,485
  • 43
  • 192
  • 261
0

you can just use list slicing with 2 as its steps :

>>> l=['x1', 'y1', 'x2', 'y2', 'x3', 'y3']
>>> l[0::2]
['x1', 'x2', 'x3']
>>> l[1::2]
['y1', 'y2', 'y3']

Or you can use itertools.cycle and go throw your list with next() :

>>> from itertools import cycle
>>> l=['x1', 'y1', 'x2', 'y2', 'x3', 'y3']
>>> c=cycle(l)

>>> l1=[]
>>> l2=[]
>>> for i in range(len(l)/2):
...  l1.append(c.next())
...  l2.append(c.next())
... 
>>> l1
['x1', 'x2', 'x3']
>>> l2
['y1', 'y2', 'y3']
Mazdak
  • 105,000
  • 18
  • 159
  • 188
0

If you have a list like

a = [1,2,3,4]

you can do

b = [a[0:len(a)-1:2], a[1:len(a):2]]

which will return a list containing two lists (kind of 2D python array)

>>> [[1, 3], [2, 4]]

This actually works if you are sure the length of your array is even.

rodfr
  • 1
  • 3
  • Make it `b = [a[0::2], a[1::2]]` and it will work on odd too. Also, it's called list, as array in Python is a completely different type. – Wikiii122 Nov 06 '14 at 19:08
  • Yours is even simpler! Sorry for the "list" vs "array" issue, I just edited it. You can still call `b[1][1]` – rodfr Nov 06 '14 at 19:40
0

Suppose a is your given list. Then you can invoke these two commands for separate lists:

b=[a [i]  for i in range (len (a)) if i % 2==0]
c=[a [i]  for i in range (len (a)) if i % 2==1]
nandhp
  • 4,680
  • 2
  • 30
  • 42
0

Adapted from the grouper recipe

def grouper(iterable,n):
    return zip(*([iter(iterable)]*n))

print grouper(range(9),3)
# [(0, 1, 2), (3, 4, 5), (6, 7, 8)]

# if len(iterable)%n : truncation
print grouped(range(14),3)
# [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)]
gboffi
  • 22,939
  • 8
  • 54
  • 85