How to get all possible combinations of a list’s elements?
Have a look at itertools.combinations:
itertools.combinations(iterable, r)
Return r length subsequences of elements from
the input iterable.Combinations are emitted in lexicographic sort order. So, if the
input iterable is sorted, the
combination tuples will be produced in
sorted order.
Since 2.6, batteries are included!
How to generate all possible combinations from the letters of strings in a list
I can propose you a trivial algorithm to do the same, it is using recursion:
def associations(entry):
while len(entry) > 2:
to_treat_later = entry.pop()
print(f"We will treat {to_treat_later} later")
entry = associations(entry)
entry.append(to_treat_later)
else:
print(f"we can start with {entry}")
associated_entry = []
for elt_1 in entry[0]:
for elt_2 in entry[1]:
associated_entry.append(f"{elt_1}{elt_2}")
return [associated_entry]
def convert_entry(entry):
converted_entry = []
for elt in entry:
list_elt = []
for letter in elt:
list_elt.append(letter)
converted_entry.append(list_elt)
return converted_entry
the_entry = ["jkl", "ghi", "def", "cfe"]
associations(convert_entry(the_entry))
Getting all possible combination for [1,0] with length 3 [0,0,0] to [1,1,1]
You're looking for a Cartesian product, not a combination or permutation of [0, 1]
. For that, you can use itertools.product
.
from itertools import product
items = [0, 1]
for item in product(items, repeat=3):
print(item)
This produces the output you're looking for (albeit in a slightly different order):
(0, 0, 0)
(0, 0, 1)
(0, 1, 0)
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)
How to generate all the possible combinations for a list of elements grouped in different sizes?
You should take a look at the more-itertools library. And more particularly to the set_partitions method that should suits your needs.
from more-itertools import set_partitions
fruits = ['Apple', 'Orange', 'Banana', 'Watermelon']
for itm in set_partitions(fruits)):
print(itm)
The ouput produce is :
[['Apple', 'Orange', 'Banana', 'Watermelon']]
[['Apple'], ['Orange', 'Banana', 'Watermelon']]
[['Apple', 'Orange'], ['Banana', 'Watermelon']]
[['Orange'], ['Apple', 'Banana', 'Watermelon']]
[['Apple', 'Orange', 'Banana'], ['Watermelon']]
[['Orange', 'Banana'], ['Apple', 'Watermelon']]
[['Apple', 'Banana'], ['Orange', 'Watermelon']]
[['Banana'], ['Apple', 'Orange', 'Watermelon']]
[['Apple'], ['Orange'], ['Banana', 'Watermelon']]
[['Apple'], ['Orange', 'Banana'], ['Watermelon']]
[['Apple'], ['Banana'], ['Orange', 'Watermelon']]
[['Apple', 'Orange'], ['Banana'], ['Watermelon']]
[['Orange'], ['Apple', 'Banana'], ['Watermelon']]
[['Orange'], ['Banana'], ['Apple', 'Watermelon']]
[['Apple'], ['Orange'], ['Banana'], ['Watermelon']]
EDIT:
And if you want all every combinations possible if you reorder your orignal list you can use the previous method in combination with the the permutations method of itertools with something like:
import itertools
from more_itertools import set_partitions
fruits = ['Apple', 'Orange', 'Banana', 'Watermelon']
output = list()
for x in itertools.permutations(fruits):
output += list(set_partitions(x))
How to get all possible combinations of array elements without duplicate
Judging from your desired result, you want all the combinations with 2 choices of 'A'
, 'B'
, 'C'
, and ''
(nothing). You can do it with nchoosek
as follows
result = nchoosek(' ABC', 2) % note space for empty
Output
result =
6×2 char array
' A'
' B'
' C'
'AB'
'AC'
'BC'
Then removing the spaces and converting the combinations to a cell array:
result = strrep(cellstr(result), ' ', '')
As Wolfie pointed out, this only works for single character input, for multi character inputs we can use string arrays instead of char arrays:
result = nchoosek(["","A1","B2","C3"], 2);
result = result(:,1) + result(:,2) % string cat
% result = cellstr(result); % optional if want cell output
result =
6×1 string array
"A1"
"B2"
"C3"
"A1B2"
"A1C3"
"B2C3"
Create all possible combinations of lists of different sizes in numpy
Similar to Olvin Roght's comment, but if you put your sublists in a list you can do:
>>>> ls = [[1,2],[3,4],[5,6,7],[8,9,10]]
>>>> [item for cmb in combinations(ls, 2) for item in product(*cmb)]
[(1, 3), (1, 4), (2, 3), (2, 4), (1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (1, 8), (1, 9), (1, 10), (2, 8), (2, 9), (2, 10), (3, 5), (3, 6), (3, 7), (4, 5), (4, 6), (4, 7), (3, 8), (3, 9), (3, 10), (4, 8), (4, 9), (4, 10), (5, 8), (5, 9), (5, 10), (6, 8), (6, 9), (6, 10), (7, 8), (7, 9), (7, 10)]
Related Topics
How to Dynamically Generate HTML Code Using .Net'S Webbrowser or Mshtml.Htmldocument
Difference Between a Field and a Property
Convert List≪Derivedclass≫ to List≪Baseclass≫
How to Find the Method That Called the Current Method
Passing Objects by Reference or Value in C#
How to Provide User Name and Password When Connecting to a Network Share
What Is the Yield Keyword Used For in C#
Filesystemwatcher Changed Event Is Raised Twice
How to Strip HTML Tags from a String in Asp.Net
How to Configure an App to Run Correctly on a Machine With a High Dpi Setting (E.G. 150%)
How to Stop Backgroundworker on Form'S Closing Event
Elevating Process Privilege Programmatically
The Located Assembly'S Manifest Definition Does Not Match the Assembly Reference