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()将数组转换为列表相关推荐

  1. python中的array函数作用_Python中的Array | 数组2(简介和功能)

    相关文章:Python中的数组Array | 1(简介和功能) 以下是更多函数. 1. typecode:此函数返回初始化数组所用的数据类型. 2. itemsize:此函数返回单个数组元素的大小(以 ...

  2. python中使用zip函数基于两个列表数据list创建字典dict数据(Create a dictionary by passing the output of zip to dict)

    python中使用zip函数基于两个列表数据list创建字典dict数据(Create a dictionary by passing the output of zip to dict) 目录

  3. python中集合set,字典dict和列表list的区别以及用法

    python中set代表集合,list代表列表,dict代表字典 set和dict的区别在于,dict是存储key-value,每一个key都是唯一的,set相对于dict存储的是key,且key是唯 ...

  4. python如何创建一个列表_使用python中的format()创建一个列表(make a list using format() in python)...

    使用python中的format()创建一个列表(make a list using format() in python) 我是python和编码的新手. 因此,如果已经讨论过这件事,我很抱歉,我无 ...

  5. python中字母数字的大小比较_python列表怎么比较大小

    Python中可以使用cmp()函数比较两个列表的大小. cmp()函数语法:cmp(list1, list2) 参数: list1 -- 比较的列表.list2 -- 比较的列表. 返回值: 如果比 ...

  6. Python中怎么通过字典的值找到列表中对应的字典,并修改

    Python中怎么通过字典的值找到列表中对应的字典,并修改? 接下来就是我的代码 # 定义一个列表,存放字典 card_list = [{"name":"小米" ...

  7. 《Python数据分析基础教程:NumPy学习指南(第2版)》笔记4:第二章 NumPy基础3——数组的组合、分割,数组转换为列表

    第二章 NumPy基础3 本章示例代码中的输入和输出均来自IPython会话. 2.7 数组的组合 NumPy数组有水平组合.垂直组合和深度组合等多种组合方式,我们将使用vstack.dstack. ...

  8. Python –将NumPy数组转换为列表

    We can use numpy ndarray tolist() function to convert the array to a list. If the array is multi-dim ...

  9. python binascii array('c')_详解Python中的array数组模块相关使用

    初始化array实例化可以提供一个参数来描述允许那种数据类型,还可以有一个初始的数据序列存储在数组中. import array import binascii s = 'This is the ar ...

最新文章

  1. XPE一般性组件整理
  2. 一文读懂Https的安全性原理、数字证书、单项认证、双项认证等
  3. 聊聊前后端分离的接口规范
  4. cannot import name #39get_all_providers#39
  5. 什么是脱离文档流?什么是文档流?
  6. UPS及蓄电池的运行管理、维护操作和常见故障处理
  7. Oracle 数据类型及存储方式
  8. java合并整形_Java:合并InputStreams(Java: merging InputStreams)
  9. 从零开始发布自己的NPM包
  10. Nginx学习之一-第一个程序Hello World
  11. 关于Linux学习的热身知识六
  12. 单链表的读取、插入与删除
  13. 透视形变(perspective distortion)
  14. [Material Design] 教你做一个Material风格、动画的按钮(MaterialButton)
  15. 23. 电容触摸按键实验
  16. 线型符号 matlab,Matlab---画图线型、符号及颜色
  17. 张学孟 (帮别人名字作诗)
  18. 现在的国产深度deepin操作系统不赖,推荐试用
  19. Android compileSdkVersion27+ was not granted this permission: android.permission.WRITE_SETTINGS.
  20. 关于SAPI的两段小代码(c++)

热门文章

  1. 真机x86 android分辨率,Android-x86入门之--启动参数设置
  2. 调用布尔变量java_关于java的参数的调用,还有布尔的理解,这有一段代码,我有些不太理解,希望能够帮我分析下,谢谢...
  3. php制图汉字,PHP用imageTtfText函数在图片上写入汉字
  4. 使用html css js实现计算器
  5. 在java web工程中jsp页面中使用kindeditor
  6. 关于Ubuntu使用笔记
  7. 推荐一些经过实践检验的学习方法
  8. 如何搭建lamp(CentOS7+Apache+MySQL+PHP)环境 [转]
  9. linux中的开机和关机命令
  10. 调光设备术语:调光曲线(转)