Obtain a list of all combinations of two elements with length n
itertools.product is what you want here:
import itertools
def combo(L,n):
return itertools.product(L, repeat=n)
For example:
>>> my_iter = product([0, 1], repeat=10)
>>> next(my_iter)
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
>>> next(my_iter)
(0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
>>> next(my_iter)
(0, 0, 0, 0, 0, 0, 0, 0, 1, 0)
>>> next(my_iter)
(0, 0, 0, 0, 0, 0, 0, 0, 1, 1)
>>> next(my_iter)
(0, 0, 0, 0, 0, 0, 0, 1, 0, 0)
>>> next(my_iter)
(0, 0, 0, 0, 0, 0, 0, 1, 0, 1)
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 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!
Listing all combinations of a list up to length n (Python)
You could use itertools.combinations
and iterate for increasing lengths:
from itertools import combinations
x = ['a','b','c','d','e']
c = []
n = 2
for i in range(n):
c.extend(combinations(x, i + 1))
print(c)
or, using a list comprehension:
from itertools import combinations
x = ['a','b','c','d','e']
n = 2
c = [comb for i in range(n) for comb in combinations(x, i + 1)]
print(c)
Find all combinations of 0, 1 that sum to 1 or 2 at varying lengths / sizes
if you don't mind using an external library (sympy
) you could use this:
from sympy.utilities.iterables import multiset_permutations
length = 4
for n in range(1, 3):
lst = [1] * n + [0] * (length - n)
for perm in multiset_permutations(lst):
print(perm)
multiset_permutations
generates all distinct permutations of a list with elements that are not pairwise different. i use this for lists with the desired numbers of 0
and 1
.
if your lists contain many elements this will be much more efficient that just go through all possible permutations and discard the duplicates using a set
.
get all words combinations and path from letters arry
After debugging, it's apparent that the result possible_words
does not contain the key (1, 0), (0, 1), (1, 2)
, so that explains why it's not part of the answer - so the question becomes why doesn't the call to coordinates_lst_and_str_lst()
generate that tuple (and the other 'missing' one)
If you break after constructing combine
in coordinates_lst_and_str_lst
, you will find that [((1, 0), 'D'), ((0, 1), 'O'), ((1, 2), 'G')]
is not in combine
, this means coordinates_lst_and_str_lst
can't find it as a solution.
So, the problem must be in create_dict
, which apparently isn't creating all the legal moves.
And indeed, in create_dict
, you use itertools.combinations()
, which gives you all the unique combinations of n
items from a collection, disregarding their order, but you care about the order.
So, you don't want itertools.combinations(new_dict.items(), n)
, you want itertools.permutations(new_dict.items(), n)
. Have a closer look at the difference between combinations and permutations (of size n).
Generating all possibly length n combinations of two items in python
With itertools.product
:
In [1]: from itertools import product
In [2]: list(product((0, 1), repeat=4))
Out[2]:
[(0, 0, 0, 0),
(0, 0, 0, 1),
(0, 0, 1, 0),
(0, 0, 1, 1),
(0, 1, 0, 0),
(0, 1, 0, 1),
(0, 1, 1, 0),
(0, 1, 1, 1),
(1, 0, 0, 0),
(1, 0, 0, 1),
(1, 0, 1, 0),
(1, 0, 1, 1),
(1, 1, 0, 0),
(1, 1, 0, 1),
(1, 1, 1, 0),
(1, 1, 1, 1)]
You can also just print ints as binary strings:
In [3]: for i in range(2**4):
...: print('{:04b}'.format(i))
...:
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Generate combinations of length k from number set N, order matters, replacement allowed
What you need is product
import itertools
N = [1, 2, 3]
y = list(itertools.product(N, N))
print(y) # [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
# ^ ^
# |_______________| he no longer thinks it's the same
Now, from your question it is not clear what you would like to do if k != len(N)
so I will leave that to you (slice N
maybe?)..
Get all combinations of an unknown length string
import itertools
s = 'abc'
for i in range(len(s)+1):
perms = itertools.permutations(s,i)
for perm in perms:
print(''.join(perm))
Output:
a
b
c
ab
ac
ba
bc
ca
cb
abc
acb
bac
bca
cab
cba
Related Topics
Split a List into Nested Lists on a Value
Python Worker Failed to Connect Back
Python: Sort Function Breaks in the Presence of Nan
Why Does Indexing Numpy Arrays with Brackets and Commas Differ in Behavior
How to Read Unicode Input and Compare Unicode Strings in Python
What's a Good Equivalent to Subprocess.Check_Call That Returns the Contents of Stdout
Weird Behavior: Lambda Inside List Comprehension
Openpyxl 1.8.5: Reading the Result of a Formula Typed in a Cell Using Openpyxl
Using Python's Multiprocessing Module to Execute Simultaneous and Separate Seawat/Modflow Model Runs
How to "Zip Sort" Parallel Numpy Arrays
How to Call Python Code from C Code
Preprocessing in Scikit Learn - Single Sample - Depreciation Warning
Django Model Field Default Based Off Another Field in Same Model
Suppress Insecurerequestwarning: Unverified Https Request Is Being Made in Python2.6