首页 > 代码库 > Python内置函数(38)——list

Python内置函数(38)——list

英文文档:

class list([iterable])

Rather than being a function, list is actually a mutable sequence type, as documented in Lists and Sequence Types — list, tuple, range

 

说明:

  1. list函数,实际是上列表类型的构造函数。

  2. 可以不传入任何参数,结果返回一个空列表。

>>> a = list()
>>> a
[]

  3. 可以传入一个可迭代对象,如字符串,字节数组、元组、列表、range对象,结果将返回可迭代对象中元素组成的列表。

>>> list(abcd) # 字符串
[a, b, c, d]
>>> list(bytes(abcd,utf-8)) # 字节数组
[97, 98, 99, 100]
>>> list((a,b,c,d)) # 元组
[a, b, c, d]
>>> list([a,b,c,d]) # 列表
[a, b, c, d]
>>> list(range(1,5)) # range对象
[1, 2, 3, 4]

 

Python内置函数(38)——list