是否有更短(非重复)的方法来创建相同浮点数的列表?

Joe*_*oey -3 python numpy shorthand

是否可以用速记写下:

x = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
Run Code Online (Sandbox Code Playgroud)

如说

x = ([0.5]*12)?
Run Code Online (Sandbox Code Playgroud)

mgi*_*son 5

如果您正在使用python的内置列表,那么您将使用的是:

x = [0.5] * 12
Run Code Online (Sandbox Code Playgroud)

如果您正在使用numpy并想要创建一个数组,那么您正在寻找的例程就是full

import numpy as np
x = np.full(12, 0.5)
Run Code Online (Sandbox Code Playgroud)