首页 > 解决方案 > 将两列中的数据保存到两个列表中

问题描述

我有两列浮点数。我需要将每列中的数据分成单独的列表(x 和 y)并绘制数据 y 与 x。我写了一些东西,但它一直给我一个错误,即 ValueError: need more than 1 value to unpack

数据文件的提取如下所示,

0.0 1.0   
0.02 1.0   
0.04 1.0  
0.06 1.0  
0.08 1.0   
0.1 1.0   
0.12 1.0   
0.14 1.0   
0.16 1.0   
0.18 1.0   
0.2 1.0  
0.22 1.0   
0.24 1.0   
0.26 1.0   
0.28 1.0   
0.3 1.0  

我的代码看起来像这样,

  import NumPy as np
  import math

  f = open('partA-imag.dat' , "r").  
  lines = f.readlines(). 
  #file.close().                                                                       
  x_axis = [].                                                                       
  y_axis = [].                                                                       
  for line in lines: 
      x,y = line.split(). 
      x_axis.append(x). 
      y_axis.append(y). 
      print(x,y). 

  print(x_axis). 
  print(y_axis). 
  plt.plot(x_axis,y_axis). 
  plt.show()

标签: pythonlistnumpymathsplit

解决方案


lines = f.readlines()
x_ = []
y_ = []
for line in lines:
    x, y = line.split(' ')
    x_.append(float(x.rstrip()))
    y_.append(float(y.rstrip()))

推荐阅读