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