0

If I have a csv file with only two rows containing an x and y coordinate, how would I go about taking row[0] and all its x points and appending them to a list?

In other words, lets say I have a list:

[[1,2], [3,5]]

Is there a way to take 1 and 3 and put them into a new list, and then 2 and 5 into another list?

2
  • So far I have only managed to get [[1,2], [3,5]], but my next step is wanting to adding the individual x and y. However, I realized that that is impossible if my list is in the form of list1 =[[1,2], [3,5]] Commented Jul 6, 2014 at 0:26
  • 1
    possible duplicate of Read columns into separate lists Commented Jul 6, 2014 at 0:37

1 Answer 1

2
points = [[1,2], [3,5]] #[[x,y],[x,y]]
x,y = zip(*points) #x = [1,3],y=[2,5]

this is an easy way to transpose any 2d array (take the rows and change them to columns)

if you had 3d points for example

points = [[1,2,3],[2,3,4],[4,5,6]]
x,y,z = zip(*points)

you can get your points from the csv

points = list(csv.reader(open("my_file.csv")))
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry, what does zip(*points) do? I am wondering how I can take points from a row in excel and sum them up in python.
you said you had a list ... that list is points ... anyway theres one way to get your points out of a csv above ...

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.