简体   繁体   English

如何在Python中打印列表的元素(数组)?

[英]How to print the elements (which are array) of list in Python?

I have some code in python which outputs the array named datafile. 我在python中有一些代码输出名为datafile的数组。 One of the element of that array is: first element for example 该数组的元素之一是:例如第一个元素

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

I like to print first and second element of the list: 我想打印列表的第一个和第二个元素:

First element: 第一个要素:

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

What's the easy way to print or separate those elements? 打印或分离这些元素的简便方法是什么? Thank you EDIT: If I do as you Joran said then, Once I can separate those elements, I want to superimpose plots of datafile[i][0] versus datafile[i][1]. 谢谢编辑:如果我按照Joran所说的那样做,那么一旦我可以分离这些元素,我想叠加datafile [i] [0]与datafile [i] [1]的关系图。

I was trying to achieve that by doing for loop: 我试图通过做循环来实现这一点:

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

But I am getting error "list indices must be integers, not tuple ". 但是我收到错误消息“列表索引必须是整数,而不是元组”。 I have been stuck on this for a while. 我被困在这一段时间了。

NEVERMIND! 没关系! I fixed it! 我修好了它! Thank you guys for your help! 谢谢你们的帮助! :) :)

ok, after the edits, I think I found the issue. 好的,在编辑之后,我想我找到了问题。

in your code: 在你的代码中:

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

You are in fact trying to use a tuple ( each datafile[i] element is a tuple) as the index when you call plot. 实际上,您在调用图时尝试使用元组(每个datafile [i]元素都是元组)作为索引。

Try to use something like: 尝试使用类似的东西:

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

And next time, try to include as many examples, code, results, etc as you can, as it will help a lot in figuring out the solution ;) 下次,请尝试尽可能多地包含示例,代码,结果等,因为这将有助于解决方案;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM