【问题标题】:How to print the elements (which are array) of list in Python?如何在 Python 中打印列表的元素(数组)?
【发布时间】:2015-02-10 23:50:48
【问题描述】:

我在 python 中有一些代码输出名为 datafile 的数组。该数组的一个元素是:例如第一个元素

datafile[0]=(array([[ 1.],
   [ 2.],
   [ 3.],
   [ 4.],
   [ 5.]]), array([[ 10.],
   [ 20.],
   [ 30.],
   [ 40.],
   [ 50.]]))

我喜欢打印列表的第一个和第二个元素:

第一个元素:

(array([[ 1.],
   [ 2.],
   [ 3.],
   [ 4.],
   [ 5.]]))

打印或分离这些元素的简单方法是什么?谢谢 编辑: 如果我按照你乔兰说的去做, 一旦我可以分离这些元素,我想叠加 datafile[i][0] 与 datafile[i][1] 的图。

我试图通过 for 循环来实现:

for i in datafile: 
     plt.plot(datafile[i][0],datafile[i][1]) 
     plt.show 

但我收到错误“列表索引必须是整数,而不是元组”。我已经坚持了一段时间。

没关系!我修好了它!谢谢你们的帮助! :)

【问题讨论】:

  • print my_array[0] ?
  • 我刚刚修改了我的问题。你能明白我的意思吗?谢谢
  • print datafile[0][0]print datafile[0][1] ?
  • print [d[0] for d in datafile]print [d[1] for d in datafile]?
  • 如果您回答了自己的问题,请将您的答案作为答案发布,以便未来的读者受益。

标签: python arrays list numpy


【解决方案1】:

好的,经过修改,我想我找到了问题。

在您的代码中:

for i in datafile: 
     plt.plot(datafile[i][0],datafile[i][1]) 
     plt.show 

实际上,当您调用 plot.

尝试使用类似的东西:

for element in datafile: 
     plt.plot(element[0],element[1]) 
     plt.show 

下一次,尝试包含尽可能多的示例、代码、结果等,因为这将有助于找出解决方案;)

【讨论】:

  • 我认为这些是 numpy 数组......所以你有更多的切片类型要覆盖:P
  • 谢谢。是的,数组中的数组中有数组。 :( 你能看到我更新的问题并帮助我进行绘图吗?谢谢
猜你喜欢
  • 2015-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 2019-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多