我这样做是为了列出滚动两个骰子的所有排列,但是我需要将其转换为所有组合的单行理解,而我很挣扎。
from itertools import product
myList = list(product(range(1,7), repeat=2))
print myList
答案 0 :(得分:0)
Itertools还具有用于允许重复元素的组合的功能:
import itertools
c = list(iter.combinations_with_replacement(range(1,7),2))
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 3), (3, 4), (3, 5), (3, 6), (4, 4), (4, 5), (4, 6), (5, 5), (5, 6), (6, 6)]