1

Initially I was simply using MatPlotLib to graph the points after I read them like this:

with open("data1.txt") as f:
    samples = f.read()

samples = samples.split('\n')

x = [row.split(' ')[0] for row in samples]
y = [row.split(' ')[1] for row in samples]

fig = plt.figure()

ax1 = fig.add_subplot(111)

ax1.plot(x,y, 'ro')

However, I now realize I need to store all that information in an array of some kind to be used later. I'm new to python, so I'm wondering either how I could access these points individually if my samples variable already has what I want, or instead create a new array or list to store all my x and y values easily.

EDIT:

This is how my txt file looks:
0.1394 0.8231
1.2149 1.8136
1.3823 0.8263
1.3726 0.8158
1.3694 0.8158
1.3855 0.8123
1.3919 0.8053
1.3694 0.8123
1.3661 0.8123
1.3597 0.7982
1.3565 0.6061
1.3468 0.7126
1.3823 0.7494

2
  • Are you looking for something of the sort [(x1,y1),(x2,y2),.....] ? Commented Feb 7, 2015 at 22:46
  • Yea exactly like that, so I guess a double array would be better then? I'm still not sure how I would do that though. Commented Feb 7, 2015 at 22:51

3 Answers 3

1

If you only have have the x and y just add them in one list comp:

with open("data1.txt") as f:
    coords = [line.split() for line in f]

print(coords)
[['0.1394', '0.8231'], ['1.2149', '1.8136'], ['1.3823', '0.8263'], ['1.3726', '0.8158'], ['1.3694', '0.8158'], ['1.3855', '0.8123'], ['1.3919', '0.8053'], ['1.3694', '0.8123'], ['1.3661', '0.8123'], ['1.3597', '0.7982'], ['1.3565', '0.6061'], ['1.3468', '0.7126'], ['1.3823', '0.7494']]

To get floats use coords = [list(map(float,line.split())) for line in f]

[[0.1394, 0.8231], [1.2149, 1.8136], [1.3823, 0.8263], [1.3726, 0.8158], [1.3694, 0.8158], [1.3855, 0.8123], [1.3919, 0.8053], [1.3694, 0.8123], [1.3661, 0.8123], [1.3597, 0.7982], [1.3565, 0.6061], [1.3468, 0.7126], [1.3823, 0.7494]]

You don't need to create two lists and then zip, just do it when your read from the file.

Sign up to request clarification or add additional context in comments.

5 Comments

This worked. I'm used to java in that I could just call coords in a format like x = coords[0] or something like that to get the 0th term in the list. What would be the syntax for that now that I have this coords list?
if you want the first element just use the same idea, your_list[0] will get the first subelement, your_list[1] will get the second etc
So if I called coords[0], would that return me just 0.1394 or 0.1394 and 0.8321. I just tested and it return both. How could I access just one of the values?
0.1394, 0.8231 if you wanted just x it would be your_list[0][0] access the subelement of the sublist
To transpose the list print(list(zip(*coords)))
0

This is how you could easily achieve it since you already have the x and y points:

a = [1,2,3]
b = [4,5,6]
print zip(a,b)

output : [(1, 4), (2, 5), (3, 6)]

Comments

0

there is a great grouper recipe

samples = samples.split() #split on all whitespace [x1,y1,x2,y2,...]
#you may want to map to int with `map(int,samples)`
coordinates = zip(*[iter(samples)]*2) #group by twos [(x1,y1),(x2,y2),...]
x,y = zip(*coordinates) # x= [x1,x2,...] ; y = [y1,y2,...]

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.