Python 中的 [0]*x 语法有什么作用?

发布于 2024-11-07 15:47:05 字数 652 浏览 1 评论 0原文

一个闪现问题,我正在查看以下代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

青巷忧颜 2024-11-14 15:47:05

[0] * x 创建一个包含 x 元素的列表。因此,

>>> [ 0 ] * 5
[0, 0, 0, 0, 0]
>>> 

请注意它们都指向同一个对象。这对于像整数这样的不可变的东西来说很酷,但对于像列表这样的东西来说却很痛苦。

>>> t = [[]] * 5
>>> t
[[], [], [], [], []]
>>> t[0].append(5)
>>> t
[[5], [5], [5], [5], [5]]
>>> 

** 运算符用于求幂。

>>> 5 ** 2 
25

The [0] * x creates a list with x elements. So,

>>> [ 0 ] * 5
[0, 0, 0, 0, 0]
>>> 

Be warned that they all point to the same object. This is cool for immutables like integers but a pain for things like lists.

>>> t = [[]] * 5
>>> t
[[], [], [], [], []]
>>> t[0].append(5)
>>> t
[[5], [5], [5], [5], [5]]
>>> 

The ** operator is used for exponentation.

>>> 5 ** 2 
25
萤火眠眠 2024-11-14 15:47:05

x = [0] * n 的演示如下:

>>> [0]*10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

它“乘以”列表元素

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

** 是幂运算符

>>> 3**2
9

虽然要小心,但它也可以是 ** kwargs(在不同的上下文中),请在此处查看更多信息 正确使用方法 * *Python 中的 kwargs

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

橘香 2024-11-14 15:47:05
  1. X =[0] * N,生成大小为 N 的列表,所有 N 个元素的值均为零。例如,X = [0] * 8,生成大小为 8 的列表。

    X = [0, 0, 0, 0, 0, 0, 0, 0]

    图形表示类似于,

结果命令[0] * 8

从技术上讲,列表的所有八个单元格都引用同一对象。这是因为列表是 Python 中的引用结构。

并且,如果您尝试向列表分配新值,例如 X[2] = 10,这在技术上不会更改现有整数实例的值。这将计算一个新整数,值为 10,并将单元格 2 设置为引用新计算的值。

图片表示,

在此处输入图像描述

  1. ** 是幂运算符,计算数字的幂。例如,5 ** 2 结果为 25。
  1. 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,

The result of command [0] * 8

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,

enter image description here

  1. ** is power operator and computes the power of a number. for example, 5 ** 2 results in 25.
不一样的天空 2024-11-14 15:47:05

X = [0] * N 创建一个 N 长度的零数组。例如:

>>> [0] * 10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

**电力运营商

>>> 2 ** 2
4

X = [0] * N creates an array of zeros of N length. For example:

>>> [0] * 10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

** is the power operator.

>>> 2 ** 2
4
深海夜未眠 2024-11-14 15:47:05

X =[0] * N 行的作用是什么?

[0] 是包含单个元素 - 0 的序列。将序列乘以 n 意味着将其连接到自身 n 次。也就是说,结果是包含 n 个零的序列。

为什么是双星号**?

它是幂运算符:b ** e = be

what does the X =[0] * N line do?

[0] is a sequence containing a single element – 0. Multiplying a sequence times n means concatenating it n times to itself. That is, the result is a sequence containing n zeros.

why the double asterisk ** ?

It’s the power operator: b ** e = be.

南渊 2024-11-14 15:47:05

1) 它初始化一个包含N个0的列表。

2) ** 是求幂运算符

1) It initialises a list containing N 0's.

2) ** is the exponentiation operator

胡渣熟男 2024-11-14 15:47:05

[0] * N 创建一个大小为 N 的列表,其中仅包含 0。

** 是一种表示法,用于将左侧进行幂运算右侧

免责声明:
[a] * N 其中 a 是对对象的引用,将复制该引用,但不会在列表内复制 a生成的

[0] * N creates a list of size N which contains only 0's

the ** is a notation for raising the left side to the power of right side

Disclaimer:
[a] * N where a is a reference to an object will copy the reference, it won't make copies of a inside the list generated

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文