How to make function foo generate all the points? [duplicate] - python

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

Related

How to randomly select two elements of a python list? [duplicate]

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))

Mathematic Operational an integer to a bunch of value or list in python? [duplicate]

This question already has answers here:
How to multiply all integers inside list [duplicate]
(6 answers)
Closed 4 years ago.
is there other way to do an matematic operational ( add, substract, multiply divided ) from an integer foreach value inside a list ?
for example:
a = 4
arr = list(20,1,5,36,10,31,100)
the a variable need to substract/multiply/divided to all values inside those list using for function like below snippet.
ar = []
for x in arr
a = 4*x
ar.append(a)
Is there any a better approach to solve this rather than using for/while loop ?
Numpy or other vectorization methods notwithstanding, you're looking for list comprehensions.
arr = list(20,1,5,36,10,31,100)
quadrupled_arr = [x * 4 for x in arr]

How to create function that returns all combinations of this list in Python? [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 4 years ago.
I have this kind of list:
[['I'],['want','love','like'],['cat',dog]]
and I want to create a function that can return all combinations of words in the lists like this:
[['I'],['want'],['cat']]
[['I'],['love'],['cat']]
[['I'],['like'],['cat']]
[['I'],['want'],['dog']]
[['I'],['love'],['dog']]
[['I'],['like'],['dog']]
PS: The function must work with any n words
import itertools
for k in itertools.product(*lst):
print(k)

how to define a list with predefined length in Python [duplicate]

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.

Calling itertools.product with unkown number of args [duplicate]

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 *

Categories