Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3中list()和[]之间的差异_Python_Python 3.x_List - Fatal编程技术网

Python 3中list()和[]之间的差异

Python 3中list()和[]之间的差异,python,python-3.x,list,Python,Python 3.x,List,在Python3中初始化列表时,list()和[]之间有什么区别吗?该方法采用序列类型并将它们转换为列表。这用于将给定的元组转换为列表 sample_touple = ('a', 'C', 'J', 13) list1 = list(sample_touple) print ("List values ", list1) string_value="sampletest" list2 = list(string_value) print ("List string values : ", li

在Python3中初始化
列表时,
list()
[]
之间有什么区别吗?

该方法采用序列类型并将它们转换为列表。这用于将给定的元组转换为列表

sample_touple = ('a', 'C', 'J', 13)
list1 = list(sample_touple)
print ("List values ", list1)

string_value="sampletest"
list2 = list(string_value)
print ("List string values : ", list2)
输出:

List values  ['a', 'C', 'J', 13]                                                                                                                              
List string values :  ['s', 'a', 'm', 'p', 'l', 'e', 't', 'e', 's', 't']   
其中
[]
直接声明为列表

sample = [1,2,3]


在功能上,它们产生相同的结果,不同的内部Python实现:

import dis


def brackets():
    return []


def list_builtin():
    return list()

它们接近相等;构造函数变量执行函数查找和函数调用;文字不存在-分解python字节码显示:

from dis import dis


def list_constructor():
    return list()

def list_literal():
    return []


print("constructor:")
dis(list_constructor)

print()

print("literal:")
dis(list_literal)
这将产生:

constructor:
  5           0 LOAD_GLOBAL              0 (list)
              2 CALL_FUNCTION            0
              4 RETURN_VALUE

literal:
  8           0 BUILD_LIST               0
              2 RETURN_VALUE

至于时间:中给出的基准可能会误导某些(可能很少)案例。例如,与此进行比较:

$ python3 -m timeit 'list(range(7))'
1000000 loops, best of 5: 224 nsec per loop
$ python3 -m timeit '[i for i in range(7)]'
1000000 loops, best of 5: 352 nsec per loop
使用
list
从生成器构建列表似乎比列表理解更快。我假设这是因为列表理解中的
for
循环是一个python循环,而在
list
版本的python解释器中的
C
实现中运行相同的循环


还请注意,
list
可以重新分配给其他对象(例如,
list=int
现在
list()
将只返回整数
0
),但您不能修改
[]

虽然这可能是显而易见的。。。为了完整性:这两个版本有一个不同的接口:接受iterable上的exactls作为参数。它将遍历它并将元素放入新列表中;文字版本没有(明确的列表理解除外):

由于
list()
是一个返回
list
对象的函数,
[]
是list对象本身,因此第二种形式更快,因为它不涉及函数调用:

> python3 -m timeit 'list()'
10000000 loops, best of 3: 0.0853 usec per loop

> python3 -m timeit '[]'
10000000 loops, best of 3: 0.0219 usec per loop

所以,如果你真的想找到一个不同,那就是。但实际上,它们是一样的。

让我们举个例子:

A = ("a","b","c")
B = list(A)
C = [C]

print("B=",B)
print("C=",C)

# list is mutable:
B.append("d")
C.append("d")

## New result
print("B=",B)
print("C=",C)

Result:
B= ['a', 'b', 'c']
C= [('a', 'b', 'c')]

B= ['a', 'b', 'c', 'd']
C= [('a', 'b', 'c'), 'd']

基于此示例,我们可以说:[]不尝试将元组转换为元素列表,而list()是尝试将元组转换为元素列表的方法。

据我所知,它们是等价的。从技术上讲,一个是返回列表中的对象的函数,另一个是文本列表对象本身。有点像
int(0)
vs
0
。实际上没有什么区别。我希望
[]
更快,因为它不涉及函数调用后的全局查找。除此之外,它是相同的;list()
带有
list=int;[]
。这是编译级别的主要区别。谢谢@jfaccioni。我添加了一个计时示例,其中
列表
更快。我承认这是一个特例。。。
print([0, 1, 2])  # [0, 1, 2]
# print(list(0, 1,2))  # TypeError: list expected at most 1 argument, got 3

tpl = (0, 1, 2)
print([tpl])           # [(0, 1, 2)]
print(list(tpl))       # [0, 1, 2]

print([range(3)])      # [range(0, 3)]
print(list(range(3)))  # [0, 1, 2]

# list-comprehension
print([i for i in range(3)])      # [0, 1, 2]
print(list(i for i in range(3)))  # [0, 1, 2]  simpler: list(range(3))
> python3 -m timeit 'list()'
10000000 loops, best of 3: 0.0853 usec per loop

> python3 -m timeit '[]'
10000000 loops, best of 3: 0.0219 usec per loop
A = ("a","b","c")
B = list(A)
C = [C]

print("B=",B)
print("C=",C)

# list is mutable:
B.append("d")
C.append("d")

## New result
print("B=",B)
print("C=",C)

Result:
B= ['a', 'b', 'c']
C= [('a', 'b', 'c')]

B= ['a', 'b', 'c', 'd']
C= [('a', 'b', 'c'), 'd']