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

这段代码展示了Python中的离散傅立叶变换(DFT)实现。`X=[0]*N`用于创建一个长度为N的全零列表X。双星号`**`在这里表示指数运算,例如`2**3`等于8。在该上下文中,它与关键字参数无关。代码还涉及时间戳记,用于计算DFT的执行时间,并提供了输出信息的选项。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值