Python 中的 [0]*x 语法有什么作用?
一个闪现问题,我正在查看以下代码
from __future__ import division
import math
import time
def dft(x, inverse = False, verbose = False) :
t = time.clock()
N = len(x)
inv = -1 if not inverse else 1
X =[0] * N
for k in xrange(N) :
for n in xrange(N) :
X[k] += x[n] * math.e**(inv * 2j * math.pi * k * n / N)
if inverse :
X[k] /= N
t = time.clock() - t
if verbose :
print "Computed","an inverse" if inverse else "a","DFT of size",N,
print "in",t,"sec."
return X
,我想知道(我不知道python):
- X =[0] * N 行是做什么的?
- 为什么有双星号 ** ?
A flash question, I'm looking at the following code
from __future__ import division
import math
import time
def dft(x, inverse = False, verbose = False) :
t = time.clock()
N = len(x)
inv = -1 if not inverse else 1
X =[0] * N
for k in xrange(N) :
for n in xrange(N) :
X[k] += x[n] * math.e**(inv * 2j * math.pi * k * n / N)
if inverse :
X[k] /= N
t = time.clock() - t
if verbose :
print "Computed","an inverse" if inverse else "a","DFT of size",N,
print "in",t,"sec."
return X
and I'm wondering (I do not know python):
- what does the X =[0] * N line do?
- why the double asterisk ** ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
[0] * x
创建一个包含x
元素的列表。因此,请注意它们都指向同一个对象。这对于像整数这样的不可变的东西来说很酷,但对于像列表这样的东西来说却很痛苦。
**
运算符用于求幂。The
[0] * x
creates a list withx
elements. So,Be warned that they all point to the same object. This is cool for immutables like integers but a pain for things like lists.
The
**
operator is used for exponentation.x = [0] * n
的演示如下:它“乘以”列表元素
**
是幂运算符虽然要小心,但它也可以是 ** kwargs(在不同的上下文中),请在此处查看更多信息 正确使用方法 * *Python 中的 kwargs
The
x = [0] * n
is demonstrated here:It 'multiplies' the list elements
The
**
is the power operatorAlthough be careful, it can also be **kwargs (in a different context), see more about that here Proper way to use **kwargs in Python
X =[0] * N
,生成大小为 N 的列表,所有 N 个元素的值均为零。例如,X = [0] * 8
,生成大小为 8 的列表。X = [0, 0, 0, 0, 0, 0, 0, 0]
图形表示类似于,
从技术上讲,列表的所有八个单元格都引用同一对象。这是因为列表是 Python 中的引用结构。
并且,如果您尝试向列表分配新值,例如
X[2] = 10
,这在技术上不会更改现有整数实例的值。这将计算一个新整数,值为 10,并将单元格 2 设置为引用新计算的值。图片表示,
**
是幂运算符,计算数字的幂。例如,5 ** 2
结果为 25。X =[0] * N
, produces a list of size N, with all N elements being the value zero. for example,X = [0] * 8
, produces a list of size 8.X = [0, 0, 0, 0, 0, 0, 0, 0]
Pictorial representation will be like,
Technically, all eight cells of the list reference the same object. This is because of the fact that lists are referential structures in python.
and, if you try to assign a new value to list, say
X[2] = 10
, this does not technically change the value of the existing integer instance. This computes a new integer, with value 10, and sets cell 2 to reference the newly computed value.Pictorial representation,
**
is power operator and computes the power of a number. for example,5 ** 2
results in 25.X = [0] * N
创建一个N
长度的零数组。例如:**
是 电力运营商。X = [0] * N
creates an array of zeros ofN
length. For example:**
is the power operator.[0]
是包含单个元素 -0
的序列。将序列乘以n
意味着将其连接到自身n
次。也就是说,结果是包含 n 个零的序列。它是幂运算符:
b ** e
= be。[0]
is a sequence containing a single element –0
. Multiplying a sequence timesn
means concatenating itn
times to itself. That is, the result is a sequence containingn
zeros.It’s the power operator:
b ** e
= be.1) 它初始化一个包含N个0的列表。
2)
**
是求幂运算符1) It initialises a list containing N 0's.
2)
**
is the exponentiation operator[0] * N
创建一个大小为 N 的列表,其中仅包含 0。**
是一种表示法,用于将左侧进行幂运算右侧免责声明:
[a] * N
其中a
是对对象的引用,将复制该引用,但不会在列表内复制a
生成的[0] * N
creates a list of size N which contains only 0'sthe
**
is a notation for raising the left side to the power of right sideDisclaimer:
[a] * N
wherea
is a reference to an object will copy the reference, it won't make copies ofa
inside the list generated