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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值