2 回答
TA贡献1865条经验 获得超7个赞
只需摆脱dicee并直接附加到dice.
def build_roll_permutations():
dice = []
for i in range(1,7):
for j in range(1,7):
dice.append((i,j))
return dice
请注意,您可以通过简单的列表理解来做到这一点
def build_roll_permuatations():
return [(i,j) for i in range(1,7) for j in range(1,7)]
或itertools.product(因为这实际上是一个产品而不是一个排列):
def build_rolls():
return list(product(range(1,7), repeat=2))
TA贡献1874条经验 获得超12个赞
修改时使用扩展而不是附加dice:
def build_roll_permutations():
dice = []
for i in range(1, 7):
dicee = []
for j in range(1, 7):
dicee.append((i, j))
dice.extend(dicee) # line to fix
return dice
print(build_roll_permutations())
输出
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]
添加回答
举报