python中θ是哪种0,[0] * x语法在Python中做什么?

这段代码实现了一个快速傅里叶变换(FFT)算法,用于信号处理。`X=[0]*N`初始化一个长度为N的全零列表作为结果数组。双星号`**`在这里表示乘方运算,例如`2**3`等于8。在Python中,`*`用于重复列表元素,如`[1, 2, 3]*3`得到 `[1, 2, 3, 1, 2, 3, 1, 2, 3]`。该代码还涉及到了时间和计算效率的度量。
摘要由CSDN通过智能技术生成

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 ** ?

解决方案

The x = [0] * n is demonstrated here:

>>> [0]*10

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

It 'multiplies' the list elements

>>> [1, 2, 3] * 3

[1, 2, 3, 1, 2, 3, 1, 2, 3]

The ** is the power operator

>>> 3**2

9

Although be careful, it can also be **kwargs (in a different context), see more about that here Proper way to use **kwargs in Python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值