[英]How do I append values from a row in a CSV file into a list in python?
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? 如果我有一个只有两行包含x和y坐标的csv文件,我将如何获取row [0]及其所有x点并将它们附加到列表中?
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? 是否可以将1
和3
放入新列表,然后将2
和5
放入另一个列表?
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 例如,如果您有3d点
points = [[1,2,3],[2,3,4],[4,5,6]]
x,y,z = zip(*points)
you can get your points
from the csv 您可以从csv中获取您的points
points = list(csv.reader(open("my_file.csv")))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.