Pythonのゼロのリスト [重複] 質問する

Pythonのゼロのリスト [重複] 質問する

ゼロのみを含むを作成するにはどうすればいいですか?それぞれにlistゼロを作成できるようにしたいですlistintrange(10)

たとえば、int範囲内のが だった場合4、次のようになります。

[0,0,0,0]

そして7

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

ベストアンサー1

#add code here to figure out the number of 0's you need, naming the variable n.
listofzeros = [0] * n

関数内に配置したい場合は、そのコードを挿入して追加するだけですreturn listofzeros

それは次のようになります:

def zerolistmaker(n):
    listofzeros = [0] * n
    return listofzeros

サンプル出力:

>>> zerolistmaker(4)
[0, 0, 0, 0]
>>> zerolistmaker(5)
[0, 0, 0, 0, 0]
>>> zerolistmaker(15)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> 

おすすめ記事