I have a list of numbers and I want to check all the combinations. How can I do it?
Let's say I have a list that contains:
1,2,3,4
Then I want to check all the combinations that are unique:
1
1,2
1,2,3
1,2,4
1,2,3,4
1,3
1,3,4
1,4
2
2,3
2,3,4
2,4
3
3,4
4
3 replies to this topic
#1
Posted 22 February 2011 - 04:17 AM
|
|
|
#2
Posted 22 February 2011 - 05:04 PM
One way would be to count, using the bits to indicate which numbers to include:
0001
0010
0011
0100
0101
...
0001
0010
0011
0100
0101
...
#3
Posted 24 February 2011 - 01:00 PM
Well haven't you studied about factorials? All possible(unique) combinations of a set of different(non-repetitive) n elements is given by n! ( n factorial) I bet there is a function in the math library for doing that but I never used it you can simply write 3 lines recursive function and you are done.
#4
Posted 24 February 2011 - 07:35 PM
python's itertools has permutations and combinations funtions
from itertools import combinations
>>> for x in range(1,5): print list(combinations([1,2,3,4], x))
...
[(1,), (2,), (3,), (4,)]
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
[(1, 2, 3, 4)]
from itertools import combinations
>>> for x in range(1,5): print list(combinations([1,2,3,4], x))
...
[(1,), (2,), (3,), (4,)]
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
[(1, 2, 3, 4)]
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









