Python 将多列文件写入数组

Python 将多列文件写入数组,python,list,Python,List,我有几个带有x和y值的文件,我想把它们同时绘制成一个图形。 为此,我想做一个循环,程序在其中写入x-data和y-data,这样我最终得到了一个如下所示的数组: results=[[x1],[y1],[x2],[y2],....] #Program to show FFT intensitys of interfering waves with phase difference path='pah_to_files' files=['0pi.txt','0.25pi.txt','0.5pi.t

我有几个带有x和y值的文件,我想把它们同时绘制成一个图形。 为此,我想做一个循环,程序在其中写入x-data和y-data,这样我最终得到了一个如下所示的数组:

results=[[x1],[y1],[x2],[y2],....]
#Program to show FFT intensitys of interfering waves with phase difference
path='pah_to_files'
files=['0pi.txt','0.25pi.txt','0.5pi.txt','0.75pi.txt','1pi.txt']

#read data
for i in range (len(files)):
    data=np.loadtxt(path+'/'+files[i], usecols=(0,1))
    position=data[:,0] #first column is position
    intensity=data[:,1] #second column is intensity
x_values = [1,2,3,4,5]
y_values = ['a','b','c','d']
pairs_of_values = zip(x_values, y_values)
之后,我想在一个图形中绘制所有数据,但使用不同的颜色。这是自动的

编辑:目前我的代码如下所示:

results=[[x1],[y1],[x2],[y2],....]
#Program to show FFT intensitys of interfering waves with phase difference
path='pah_to_files'
files=['0pi.txt','0.25pi.txt','0.5pi.txt','0.75pi.txt','1pi.txt']

#read data
for i in range (len(files)):
    data=np.loadtxt(path+'/'+files[i], usecols=(0,1))
    position=data[:,0] #first column is position
    intensity=data[:,1] #second column is intensity
x_values = [1,2,3,4,5]
y_values = ['a','b','c','d']
pairs_of_values = zip(x_values, y_values)

我知道此循环读取文件,但它总是覆盖以前的位置和强度数据。

以下行可用于按您所说的结果应有的方式显示结果列表:

results = [];
for x in range(10):
    y = x*x
    results.append([x])
    results.append([y])

print results

更好的方法是使用
zip
,如下所示:

results=[[x1],[y1],[x2],[y2],....]
#Program to show FFT intensitys of interfering waves with phase difference
path='pah_to_files'
files=['0pi.txt','0.25pi.txt','0.5pi.txt','0.75pi.txt','1pi.txt']

#read data
for i in range (len(files)):
    data=np.loadtxt(path+'/'+files[i], usecols=(0,1))
    position=data[:,0] #first column is position
    intensity=data[:,1] #second column is intensity
x_values = [1,2,3,4,5]
y_values = ['a','b','c','d']
pairs_of_values = zip(x_values, y_values)

现在,
成对的值[0]
将是一个元组
(1,'a')

对不起,我已经使用Python 4天了,我想我必须更加小心列表和数组。。。。下面的代码解决了我读取文件的问题

#Program to show FFT intensitys of interfering waves with phase difference
path='pah_to_files'
files=['0pi.txt','0.25pi.txt','0.5pi.txt','0.75pi.txt','1pi.txt']

results=[]

#read data
for i in range (len(files)): #all files which are tortured the folloing way
data=np.loadtxt(path+'/'+files[i], usecols=(0,1))

results.append(data[:,0]) #x-position
results.append(data[:,1]) #intensity

你能举例说明你的意见吗?你的预期产出是什么?到目前为止你试过什么吗?关于如何编写一个好问题的一个很好的提示是,您是否希望数组数据看起来像这样?如果您使用matplotlib来绘图,那么它需要一个X数组和一个单独的Y数组。