1
我想生成一個列表,其中包含漸進數量的隨機生成的二進制值。生成具有指定長度的嵌套列表
如何添加一個條件,告訴python將隨機值添加到列表中,直到它達到指定的長度?在這種情況下,每個新列表的長度應該是一個逐漸增大的奇數。
from random import randint
shape = []
odds = [x for x in range(100) if x % 2 == 1]
while len(shape) < 300:
for x in odds:
randy = [randint(0,1)] * x ?? # need to run this code x amount of times
shape.append(randy) # so that each len(randy) = x
*我寧願不使用count + = 1
期望的輸出:
形狀 [[0],[0,1,0], [1,1,0,1,0],[1,0,0,0,1,1,0] ... etc]
如果x%2 == 1''變爲'賠率= [x for range in(1,100,2)]'',則賠率= [x for範圍(100)它使用從1到100的一系列奇數(2是「step」或「interval」)。 – 2013-02-26 21:41:52
+1好點。我會用它。 – 2013-02-26 21:57:48