Apr-19-2020, 09:22 PM
(Apr-19-2020, 06:44 PM)PUP280 Wrote:my_example = [(x,v,w) for x in list(range(6)) for y in list(range(7)) for z in list(range(8))]Here, I have three different ranges (6, 7 and 8). If I use itertools, I can use only one, no ?
True, but it's pretty simple to trim out what you don't need. For instance, if I generate all combinations of 0-4 with 3 digits in increasing order:
Output:>>> list(combinations(range(5), 3))
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
But I don't want any where the middle digit is 2.Output:>>> [ x for x in combinations(range(5), 3) if x[1] != 2]
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 3, 4), (1, 3, 4), (2, 3, 4)]
Quote:With this code, for example, I have as result :
Output:[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (1, 0, 0) [...]
I don't understand. What code does that? The my_example code you have above doesn't run, but if you change the (x,v,w) to (x,y,z), then it prints out something that looks reasonable.
Output:>>> [(x,y,z) for x in range(6) for y in range(7) for z in range(8)]
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 0, 5), (0, 0, 6), (0, 0, 7), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 1, 5), (0, 1, 6), (0, 1, 7), (0, 2, 0), (0, 2, 1), (0, 2, 2), (0, 2, 3), (0, 2, 4), (0, 2, 5) ...
(5, 5, 0), (5, 5, 1), (5, 5, 2), (5, 5, 3), (5, 5, 4), (5, 5, 5), (5, 5, 6), (5, 5, 7), (5, 6, 0), (5, 6, 1), (5, 6, 2), (5, 6, 3), (5, 6, 4), (5, 6, 5), (5, 6, 6), (5, 6, 7)]
Quote:My wish is to know :
1/ how can I generate only one time (0,0,0) with no repetition ?
It looks like you've done that above with your my_example code. Can you run it and see how you want it to be different?
Quote:2/ in the same way, how can I generate them with only three different digits (ie (1,2,3)) ?I think you need to be more explicit. combinations() will give you sets without repetition. Can you give an example list and detail how it differs from something like combinations(range(8), 3)?