array.tolist_在Python中使用array.tolist()将数组转换为列表
array.tolist
Given an array with some elements and we have to convert them to the list using array.tolist() in Python.
給定一個包含一些元素的數組,我們必須使用Python中的array.tolist()將它們轉換為列表。
創建一個數組 (Creating an array)
To create an array - we are using "array" module in the program by importing using "import array as arr" (Read more: Create array using array module).
要創建數組-我們在程序中使用“數組”模塊,方法是使用“將數組導入為arr” (更多信息: 使用數組模塊創建數組)進行 導入 。
將數組轉換為具有相同元素的列表 (Converting array to the list with same elements)
To convert an array to the list - we use tolist() methods of "array" class, it returns the list with the same elements.
要將數組轉換為列表 -我們使用“ array”類的tolist()方法,它將返回具有相同元素的列表。
Syntax:
句法:
list = array.tolist()Example:
例:
Here, we are declaring an array of signed int named a (by using i type_code) and initializing it with the elements [10, -20, 30, -40, 50]
在這里,我們聲明一個名為a的帶符號int數組(通過使用i type_code),并使用元素[10,-20,30, -40,50]對其進行初始化。
Then, we are printing the type of elements of a and elements of array a
然后,我們打印a的元素類型和數組a的元素
Then, we declared an empty list named list1
然后,我們聲明了一個名為list1的空列表
Then, we are converting the array to the list with some elements
然后,我們將數組轉換為包含一些元素的列表
Then, we are printing the type of list1 and elements of list1
然后,我們打印列表1的類型和列表1的元素
Program:
程序:
# importing array class to use array import array as arr# declare array a = arr.array ("i", [10, -20, 30, -40, 50]) # print the type of a print ("type of a: ", type(a)) # print array print ("a is:", a);# declare an empty list list1 = list() # convert array and assign to the list list1 = a.tolist() # print type of list1 print ("Type of list1: ", type(list1)) # print list print ("list1 is: ", list1)Output
輸出量
type of a: <class 'array.array'> a is: array('i', [10, -20, 30, -40, 50]) Type of list1: <class 'list'> list1 is: [10, -20, 30, -40, 50]翻譯自: https://www.includehelp.com/python/convert-an-array-to-the-list-using-array-tolist-in-python.aspx
array.tolist
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的array.tolist_在Python中使用array.tolist()将数组转换为列表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原神壶灵阿嘟在哪
- 下一篇: 国服什么时候出女忍者呢???