如何在Python中打印数组

在这篇文章中,我们将看到如何在Python中打印数组。
众所周知,Python没有’t have an 在 -built array data type, so we try to use 列表 data type as an array. We can also use the NumPy module for creating NumPy array and apply array operation on it.

现在,我们将首先了解如何打印 列表 as an array, and then we will see how to print NumPy array 在 Python.

列印清单

使用print()

Here, 打印 () 功能 is used to print the whole array along with [].

下面是给出的Python代码:

输出:

As you can see here, rr is one dimension array and you just need to pass it to 打印 () method.

Similiarly, rr 2d is two dimensional array and represents list of lists. You need to pass it to 打印 () method to print 2D array in Python.

使用map()

If you directly use print() method to print the elements, then it will printed with [].

In case, you want to print elements in desired way, you can use 地图 () method with 加入 () method.

地图 () method will convert each item to string and then use 加入 () method to join the strings with delimeter.

这是一个例子

下面是给出的Python代码:

输出:

按开箱清单

You can use *list to print list of elements without brackets in Python 3.X.

*list 只需解压缩列表并将其传递给打印功能即可。
下面是给出的Python代码:

输出:

If you are using Python 2.x then you can import print function as below:

使用循环

在这里,for循环用于遍历数组的每个元素,然后打印该元素。我们也可以使用while循环代替for循环。

下面是给出的Python代码:

输出:

Here, we traversed to one dimenstional array rr and two dimensional array rr 2d and print its elements.

打印数字数组

使用print()

Here, 打印 () 功能 is used to print the whole NumPy array along with [].

下面是给出的Python代码:

输出:

Here rr and rr 2d 是 one dimensional numpy array and two dimensional numpy array respectively. You need to pass it to 打印 () method to print the array.

使用循环

Here, for loop is used to iterate through every element of a NumPy-array then print that element. We can also use while loop in place of for loop.

下面是给出的Python代码:

输出:

那’关于如何在Python中打印Array的所有内容。

相关文章

发表评论

您的电子邮件地址不会被公开。 必需的地方已做标记 *

订阅我们的新闻

获取质量教程到您的收件箱。现在订阅。