This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 8 years ago.
Is there an easy way to write a generator to generate all lists that are k elements long, where each element ranges from 1 to n?
So if k=3 and n=3, it would generate
[1,1,1]
[1,1,2]
[1,1,3]
[1,2,1]
[1,2,2]
[1,2,3]
[1,3,1]
...
[3,3,1]
[3,3,2]
[3,3,3]
Using itertools.product():
import itertools
gen = itertools.product(range(1, k+1), repeat=n)
This will generate tuples, if you want lists instead you can use itertools.imap() or a generator expression, for example:
gen = (list(t) for t in itertools.product(range(1, k+1), repeat=n))
Related
This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 6 months ago.
I want to be able to produce all possible permutations of an array of n elements, where each one of the n elements can take specific discrete values.
For example, lets say I want to create all possible (27) permutations of an array of 3 numbers, where:
The first number can be either 1,3 or 4
The second number can be either 0,2 or 3
The third number can be either 1,2 or 5
The answer will be
(1,0,1)
(1,2,1)
(3,0,1)
(3,2,1)
(3,2,2)
...
etc
Is anybody kind enough to tell me how to do this in Python? What I want is to provide as inputs the possible choices for each number and then the algorithm will produce all possible permutations
From your description, it looks like you are looking for the cartesian product of the three sets of numbers. The itertools module has a product function to generate the cartesian product -
from itertools import product
l1 = [1, 3, 4]
l2 = [0, 2, 3]
l3 = [1, 2, 5]
list(product(l1, l2, l3))
Output
print(len(list(product(l1, l2, l3))))
# 27
This question already has answers here:
Find indexes of common items in two python lists
(3 answers)
Closed 1 year ago.
Assume we have two lists, A and B. Is there a way to get the indices of elements in the list B, which are in the list A?
For example:
A = [1,2,3,4]
B = [3,4,1,2,5]
The result should be:
[2,3,0,1]
Could it be implemented without for-loop (or fast)?
This should work for your use case:
result = [A.index(x) for x in B if x in A]
Use index function
A = [1,2,3,4]
B = [3,4,1,2,5]
lst=[]
for i in A:
res=B.index(i)
lst.append(res)
print(lst)
# [2, 3, 0, 1]
This question already has answers here:
How to emulate sum() using a list comprehension?
(10 answers)
Closed 4 years ago.
l = [5, 7, 8, 2, 1, 4]
sum = 0
for i in l:
sum += i
print(sum)
how can I get sum of all elements but using list comprehension?
list comprehension should be used only for generating a list. You can simply use sum(l).
List comprehension always produces another list so I am not sure if you can even do that. You will surely need other function to fold the sequence into a single value.
This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 6 years ago.
Suppose I have a list of integers:
myList = [1,2,3,4]
How would one return every possible combination of these two subsets with Python.
For example, set [1,2,3,4] would produce
[1,2], [3,4] and [1,3], [2,4] and [1,4],[2,3]
itertools can give you all combinations of a list of size you define (here n):
import itertools
myList = [1,2,3,4]
n=3
for subset in itertools.combinations(myList , n):
print(subset)
output:
(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)
This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 9 years ago.
Edit:
This is not a exact duplicate of How to get all possible combinations of a list’s elements?
This topic is about finding unique combinations while the other topic is about finding ALL combinations.
If I have a python list:
L = [1,2,3,4]
what's the best way to get all the possible unique combinations of 3 elements from the list like below:
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
The order of the elements in the combinations doesn't matter. For example, "1,2,3" and "3,2,1" will be considered the same combination.
I can probably write a few loops to do this but I think there might be a one-liner which can do the same.
You need itertools.combinations:
>>> from itertools import combinations
>>> L = [1, 2, 3, 4]
>>> [",".join(map(str, comb)) for comb in combinations(L, 3)]
['1,2,3', '1,2,4', '1,3,4', '2,3,4']