简体   繁体   English

并排排列和打印 numpy 数组值

[英]arranging and printing numpy arrays values side by side

i am facing problem on printing the values obtained from below arrays values side by side.Actually i have two arrays like我在并排打印从下面的数组值获得的值时遇到问题。实际上我有两个数组,如

            x=np.arange(1.0,10.0,0.5)
            y=np.arange(1.0,10.0,0.4)

and i want to print x and y values like as follows我想打印 x 和 y 值,如下所示

    x        y

   1.0      1.0
   1.5      1.4
   2.0      1.8   
   2.5      2.2
   3.0      2.6
   ...      ...

i tried like import numpy as np x=np.arange(1.0,10.0,0.5) y=np.arange(1.0,10.0,0.4) print(x,y)我试过像 import numpy as np x=np.arange(1.0,10.0,0.5) y=np.arange(1.0,10.0,0.4) print(x,y)

but it doesnot print what i want can anybody help me on carrying this work out.Thanks.但它没有打印我想要的东西,谁能帮我完成这项工作。谢谢。

You can iterate through elements of both arrays by using zip and then print out the elements.您可以使用 zip 遍历两个数组的元素,然后打印出元素。

import numpy as np

x=np.arange(1.0,10.0,0.5)
y=np.arange(1.0,10.0,0.4)

print("x   y")
for a,b in zip(x,y):
    print(a,b)

However, this will only work if both arrays are the same size.但是,这仅在两个数组大小相同时才有效。 If the arrays are not the same size you may want to look into itertools.zip_longest https://docs.python.org/3/library/itertools.html#itertools.zip_longest .如果数组大小不同,您可能需要查看 itertools.zip_longest https://docs.python.org/3/library/itertools.html#itertools.zip_longest

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

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