This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 9 years ago.
I hope this is a easy one.
Is there any possiblity to call itertools.product with a yet (not hardcoded) not knwon number of arguments?
Something like this:
itertools.product(x[0],x[1],x[2],....)
and the dimension of x can't be hardcoded
Thanks!
Try:
itertools.product(*x)
i.e. we unpack the argument list.
You can use
itertools.product(*x)
Lookup *args and **kwargs?
a = [1,2,3]
b = [2,3,4]
c= [a,b]
itertools.product(*c)
You can pass array of arguments using *
Related
This question already has answers here:
What does the random.sample() method in Python do?
(4 answers)
Closed 2 years ago.
mylist = ['a','b','c','d']
How can I index the list so that I choose only two of the four elements? mylist[] throws all sorts of errors depending what datatype I put in the square brackets for indexing
You can use random.sample Try this:
import random
mylist = ['a','b','c','d']
print(random.sample(mylist, 2))
This question already has answers here:
Python Reverse Generator
(4 answers)
Closed 3 years ago.
I want to get the elements from a generator in inverse order like it is possible from a list.
Does an approach like [::-1] exist to achieve that? Something like:
for x in map(int,['1','2','3'])[::-1]:
print(x)
The expected is result is:
3,2,1
Just convert it back to a list first. You can't go the other direction with a generator:
for x in list(map(int,['1','2','3']))[::-1]:
print(x)
This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 7 years ago.
I have a list of lists, and I would like to enter them into a function in such a way that each element of the main list is a different argument to the function.
squares = [[1,5,9,13], [2,6,10,14], [3,7,11,15], [4,8,12,16]]
print zip(squares[0], squares[1], squares[2], squares[3])
# displays [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
Is there a way to do something like print zip(arguments(squares)) so that the arguments can be entered dynamically?
This is what the * operator is for:
print zip(*squares)
This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 8 years ago.
Here is my code:
point = (3,6,9) # let all the elements of the tuple be none negative integers
def foo(point):
for i in range(point[0]+1):
for j in range(point[1]+1):
for k in range(point[2]+1):
yield (i,j,k)
My question is:
What if I don't know the length of the tuple in advance?
How to make function foo take any tuple as argument and do the same thing? e.g. what if point = (3,6,9,0,7) ?
Use itertools.product() instead:
from itertools import product
def foo(point):
for combo in product(*(range(i + 1) for i in point)):
yield combo
This question already has answers here:
Create a list with initial capacity in Python
(11 answers)
Closed 9 years ago.
I want to do the following :
l = list()
l[2] = 'two'
As expected, that does not work. It returns an out of range exception.
Is there any way to, let say, define a list with a length ?
Try this one
values = [None]*1000
In place of 1000 use your desired number.