Apr-20-2020, 07:42 PM
Quote:Can you explain how this code works ?
I’ve tested it, am I right if I deduce it means that y is always superior to x and grows up while y < 8 ? (because y stopped at 7 and x at 6)
The outer loop is simple. It just loops from 0 to 5. But the limits of the inner loops are determined when they fire. So instead of starting at 0, it starts at whatever x+1 evaluates to. Yes, this means that the numbers will alway be increasing within a tuple. x < y < z.
In this code
[(x,y,z) for x in range(6) for y in range(7) for z in range(8)]there are no such restrictions. x, y, and z can be larger, smaller, or equal, so you get all the permutations, not all the combinations
Quote:What can I do if I want python considers that these six possibilities are equals and gives me only one time the (1,2,3) possibility in output ?
Ask for all the combinations.
>>> from itertools import combinations >>> list(combinations([1,2,3], 3)) [(1, 2, 3)] >>> list(combinations([1,2,3,4], 3)) [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]