Combinations with limited repeats in Python
问题 I know how to get ALL combinations of a list in Python with itertools, but what if I want to limit the amount of repeats? So, if I have [1, 2, 3, 4, 5] But I want to limit combinations to only 3 repeats of each item (with a fixed length of the final list, say 10): [1, 1, 1, 2, 3, 3, 5, 5, 5, 4] [1, 2, 3, 3, 3, 4, 5, 5, 4, 4] [4, 4, 1, 1, 1, 5, 2, 2, 2, 3] and so on. How do I do this? 回答1: This would work: import random L = [1, 2, 3, 4, 5] L3 = L * 3 random.shuffle(L3) L3[:10] 回答2: I don't