在这篇文章中,我们将看到如何在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.
NumPy array
在 Python.
列印清单
使用print()
Here, 打印 ()
功能 is used to print the whole array along with []
.
下面是给出的Python代码:
1 2 3 4 5 6 7 8 9 10 11 |
#整数列表 rr = [10, 20, 30, 40, 50, 60] 打印 ( “大批 :” , rr ) #清单清单 rr 2d = [ [10, 20], [30, 40] ] 打印 (“ 2暗-数组:”, rr 2d) |
输出:
1 2 3 4 |
大批 : [10, 20, 30, 40, 50, 60] 2 暗淡 - 大批 : [[10, 20], [30, 40]] |
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代码:
1 2 3 4 5 6 7 8 9 |
#整数列表 rr = [10, 20, 30, 40, 50, 60] 打印 ( '' . 加入 ( 地图 (力量, rr ))) #在新行中 打印 ( '\ n' . 加入 ( 地图 (力量, rr ))) |
输出:
1 2 3 4 5 6 7 8 9 |
10 20 30 40 50 60 10 20 30 40 50 60 |
按开箱清单
You can use *list to print list of elements without brackets in Python 3.X
.
1 2 3 |
打印 (* 列表 ) |
*list
只需解压缩列表并将其传递给打印功能即可。下面是给出的Python代码:
1 2 3 4 5 6 7 8 9 |
#在Python中打印数组 rr = [20, 40, 80, 100, 120] 打印 (* rr ) #在新行中打印数组 打印 (* rr , 九月 = '\ n' ) |
输出:
1 2 3 4 5 6 7 8 |
20 40 80 100 120 20 40 80 100 120 |
If you are using Python 2.x
then you can import print function as below:
1 2 3 |
从 __未来__ 进口 打印 _ 功能 |
使用循环
在这里,for循环用于遍历数组的每个元素,然后打印该元素。我们也可以使用while循环代替for循环。
下面是给出的Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#整数列表 rr = [10, 20, 30, 40, 50, 60] 打印 (“数组元素是:”) 为了 ele 在 rr : #单行打印 打印 ( ele , 结尾 = ” ) 打印 () #清单清单 rr 2d = [ [10, 20], [30, 40] ] 打印 (“ 2个Dim-array的元素是:”) 为了 物品 在 rr 2d: 为了 ele 在 物品 : #单行打印 打印 ( ele , 结尾 = ” ) 打印 () |
输出:
1 2 3 4 5 6 7 |
大批 元素 是 : 10 20 30 40 50 60 元素 的 2 暗淡 - 大批 是 : 10 20 30 40 |
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代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#import numpy 进口 麻木 #整数数组 rr = 麻木 . 大批 ([10, 20, 30, 40, 50, 60]) 打印 ( “大批 :” , rr ) #打印2D数组 rr 2d = 麻木 . 大批 ([[10, 20], [30, 40], [50, 60]]) 打印 (“ 2昏暗-数组:\ n”, rr 2d) |
输出:
1 2 3 4 5 6 7 |
大批 : [10 20 30 40 50 60] 2 暗淡 - 大批 : [[10 20] [30 40] [50 60]] |
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代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#import numpy 进口 麻木 #整数数组 rr = 麻木 . 大批 ([10, 20, 30, 40, 50, 60]) 打印 (“数组元素是:”) 为了 ele 在 rr : #单行打印 打印 ( ele , 结尾 = ” ) 打印 () #2暗阵列 rr 2d = 麻木 . 大批 ([[10, 20], [30, 40], [50, 60]]) 打印 (“ 2个Dim-array的元素是:”) 为了 物品 在 rr 2d: 为了 ele 在 物品 : #单行打印 打印 ( ele , 结尾 = ” ) 打印 () |
输出:
1 2 3 4 5 6 7 8 |
大批 元素 是 : 10 20 30 40 50 60 元素 的 2 暗淡 - 大批 是 : 10 20 30 40 50 60 |
那’关于如何在Python中打印Array的所有内容。