This question already has answers here:
How to use list comprehension with .extend list method? [duplicate]
(2 answers)
How can I use a list comprehension to extend a list in python? [duplicate]
(6 answers)
Python: How to extend or append multiple elements in list comprehension format?
(2 answers)
Closed 4 years ago.
Is there a 1-liner equivalent (using list comprehension) for the following:
a = []
for i in range(6):
a.extend(((-i,i,0,2),(-i-1,i,0,6)))
a = tuple(a)
I was thinking something like
tuple(((-i,i,0,2),(-i-1,i,0,6)) for i in range(6))
but this gives:
(((0, 0, 0, 2), (-1, 0, 0, 6)),
((-1, 1, 0, 2), (-2, 1, 0, 6)),
((-2, 2, 0, 2), (-3, 2, 0, 6)),
((-3, 3, 0, 2), (-4, 3, 0, 6)),
((-4, 4, 0, 2), (-5, 4, 0, 6)),
((-5, 5, 0, 2), (-6, 5, 0, 6)))
which is not what I want.
Desired output
((0, 0, 0, 2),
(-1, 0, 0, 6),
(-1, 1, 0, 2),
(-2, 1, 0, 6),
(-2, 2, 0, 2),
(-3, 2, 0, 6),
(-3, 3, 0, 2),
(-4, 3, 0, 6),
(-4, 4, 0, 2),
(-5, 4, 0, 6),
(-5, 5, 0, 2),
(-6, 5, 0, 6))
You can use a nested list comprehension.
>>> [t for i in range(6) for t in ((-i,i,0,2), (-i-1,i,0,6))]
>>>
[(0, 0, 0, 2),
(-1, 0, 0, 6),
(-1, 1, 0, 2),
(-2, 1, 0, 6),
(-2, 2, 0, 2),
(-3, 2, 0, 6),
(-3, 3, 0, 2),
(-4, 3, 0, 6),
(-4, 4, 0, 2),
(-5, 4, 0, 6),
(-5, 5, 0, 2),
(-6, 5, 0, 6)]
It reads like this
[what I want (t) | for loops as if writing non-listcomp code]
and is thus equivalent to
result = []
for i in range(6):
for t in ((-i,i,0,2), (-i-1,i,0,6)):
result.append(t)
Related
I'd like to make nested list
given_list = [[0, 1, 2], [0, 1, 2], [0, 1, 2]] # each element : range(0, n), num of element : m
new_list = [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], ..., [2, 2, 2]] # total num : n^m
How do I make it?
I tried to overlap the for statement m times, but I don't think it's pythonic.
Looks like you are trying to compute the product of the lists in given_list:
> from itertools import product
> list(product(*given_list))
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 2, 0), (0, 2, 1), (0, 2, 2), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 2, 0), (1, 2, 1), (1, 2, 2), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 1, 0), (2, 1, 1), (2, 1, 2), (2, 2, 0), (2, 2, 1), (2, 2, 2)]
If you really need a list of lists, rather than a list of tuples, you'll have to call list on each element.
[list(t) for t in product(*given_list)]
This question already has answers here:
Python itertools permutations how to include repeating characters [duplicate]
(2 answers)
How to get the cartesian product of multiple lists
(17 answers)
Closed 5 years ago.
from itertools import permutations
l = [0, 1, 2, 3, 4]
x = permutations (l, 3)
I get the following :
(0, 1, 2) , (0, 1, 3), ...., (0, 2, 1), (0, 2, 3), (0,2,4),...., (4, 3, 0), (4, 3, 1),
(4, 3, 2)
Which is what was expected.
But what i need is :
(0, 0, 0), (0, 0, 1), ...., (0, 0, 4), (0, 1, 0), (0, 1, 1)........
How to achieve this ?
What you need is a permutation with replacement, or a product, but itertool's permutations produces permutations without replacement. You can calculate the product yourself:
[(x,y,z) for x in l for y in l for z in l]
#[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 1, 0), ...
Or use the namesake function from itertools:
list(itertools.product(l,repeat=3))
# [(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 1, 0),...
The latter approach is more efficient.
You need to use product , not using permutations, from itertools module like this example:
from itertools import product
l = [0, 1, 2, 3, 4]
# Or:
# b = list(product(l, repeat=3))
b = list(product(l,l,l))
print(b)
Output:
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), ..., (4, 4, 1), (4, 4, 2), (4, 4, 3), (4, 4, 4)]
You need product and not permutation
from itertools import product
l = [0, 1, 2, 3, 4]
b = list(product(l, repeat=3))
I am trying to get the list of three-element tuples from the list [-4, -2, 1, 2, 5, 0] using comprehensions, and checking whether they fulfil the condition sum([] == 0). The following code works. However, there is no question that there ought to be an easier, much more elegant way of expressing these comprehensions:
[
(i, j, k) for i in [-4, -2, 1, 2, 5, 0]
for j in [-4, -2, 1, 2, 5, 0]
for k in [-4, -2, 1, 2, 5, 0] if sum([i, j, k]) == 0
]
Output:
[(-4, 2, 2), (-2, 1, 1), (-2, 2, 0), (-2, 0, 2), (1, -2, 1),
(1, 1, -2), (2, -4, 2), (2, -2, 0), (2, 2, -4), (2, 0, -2),
(0, -2, 2), (0, 2, -2), (0, 0, 0)]
The question is searching for an expression like (i, j, k) for i, j, k in [-4, -2, 1, 2, 5, 0].
You can use itertools.product to hide the nested loops in your list comprehension. Use the repeat parameter to set the number of loops over the list (i.e. the number of elements in the tuple):
>>> import itertools
>>> lst = [-4, -2, 1, 2, 5, 0]
>>> [x for x in itertools.product(lst, repeat=3) if sum(x) == 0]
[(-4, 2, 2),
(-2, 1, 1),
(-2, 2, 0),
(-2, 0, 2),
(1, -2, 1),
(1, 1, -2),
(2, -4, 2),
(2, -2, 0),
(2, 2, -4),
(2, 0, -2),
(0, -2, 2),
(0, 2, -2),
(0, 0, 0)]
Python:
How to efficiency execute a multidimensional loop, when the number of indexes to loop is dynamic.
Assume an array var_size containing the size of each variable
var_size = [ 3, 4, 5 ]
and a function 'loop' which will call 'f(current_state)' for each point.
def f(state): print state
loop(var_size, f)
This call would call f in the following order:
f( [ 0, 0, 0])
f( [ 0, 0, 1])
f( [ 0, 0, 2])
f( [ 0, 1, 0])
etc....
You can do this with itertools.product:
>>> print list(itertools.product(*(range(x) for x in reversed([3,4,5]))))
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 2, 0), (0, 2, 1), (0, 2, 2), (0, 3, 0), (0, 3, 1), (0, 3, 2), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 2, 0), (1, 2, 1), (1, 2, 2), (1, 3, 0), (1, 3, 1), (1, 3, 2), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 1, 0), (2, 1, 1), (2, 1, 2), (2, 2, 0), (2, 2, 1), (2, 2, 2), (2, 3, 0), (2, 3, 1), (2, 3, 2), (3, 0, 0), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 1), (3, 1, 2), (3, 2, 0), (3, 2, 1), (3, 2, 2), (3, 3, 0), (3, 3, 1), (3, 3, 2), (4, 0, 0), (4, 0, 1), (4, 0, 2), (4, 1, 0), (4, 1, 1), (4, 1, 2), (4, 2, 0), (4, 2, 1), (4, 2, 2), (4, 3, 0), (4, 3, 1), (4, 3, 2)]
Note that I'm generating tuples instead of lists, but that's easy to fix if you really need to.
So, to me it looks like you want:
map(f,itertools.product(*map(range,reversed(var_size))))
Make a list initialized to 0s, as many entries as are in var_size. We treat this list as a list of 'tumblers' - we increment the last one in the list until it overflows its limit (aka var_size at the same point into the list). If so, we set it to 0, go one left and repeat the increment/overflow check until we either do not overflow (reset the 'which tumbler are we looking at' variable back to the last and continue) or overflow all entries of the list (we're done, we looped all the way around), then perform the next call.
I don't know if this is optimal or pythonic, but it is O(n).
This code does the job - And it has the advantage of not creating the list. However, it not that elegant....
Any ideas on how to get this better?
def loop(var_size, f):
nb = len(var_size)
state = [0]*nb
ok = True
while ok:
f(state)
for i in range(nb-1, -1, -1):
state[i] = state[i]+1
if state[i] < var_size[i]:
break
else:
if i == 0:
ok = False
break
else:
state[i] = 0
var_size = [3,4,5]
def f(state):
print state
loop(var_size, f)
I am trying to understand how to write code that will output all the divisors of a number. The approach that I am most interested in taking begins with a function that returns a dictionary where the keys are the prime divisors and the values are the number of times divisible. I have already written this function like so:
def div_pair(num):
divPair = {}
for prime in prime_gen():
primeDegree = 0
while num % prime == 0:
num = int(num / prime)
primeDegree += 1
if primeDegree > 0:
divPair[prime] = primeDegree
if num == 1:
return divPair
As an example, the number 84,000 outputs the dictionary
{2: 5, 3: 1, 5: 3, 7: 1}
What I want to do from here is generate powersets(?) of any given values returned by the different numbers divPair would return, and then multiply these powersets by their matched primes. This is an example which uses the kind of code I am trying to use to generate the powersets:
from itertools import product
list(product(range(5+1), range(1+1), range(3+1), range(1+1)))
Outputs this:
[(0, 0, 0, 0),
(0, 0, 0, 1),
(0, 0, 1, 0),
(0, 0, 1, 1),
(0, 0, 2, 0),
(0, 0, 2, 1),
(0, 0, 3, 0),
(0, 0, 3, 1),
(0, 1, 0, 0),
(0, 1, 0, 1),
(0, 1, 1, 0),
(0, 1, 1, 1),
(0, 1, 2, 0),
(0, 1, 2, 1),
(0, 1, 3, 0),
(0, 1, 3, 1),
(1, 0, 0, 0),
(1, 0, 0, 1),
(1, 0, 1, 0),
(1, 0, 1, 1),
(1, 0, 2, 0),
(1, 0, 2, 1),
(1, 0, 3, 0),
(1, 0, 3, 1),
(1, 1, 0, 0),
(1, 1, 0, 1),
(1, 1, 1, 0),
(1, 1, 1, 1),
(1, 1, 2, 0),
(1, 1, 2, 1),
(1, 1, 3, 0),
(1, 1, 3, 1),
(2, 0, 0, 0),
(2, 0, 0, 1),
(2, 0, 1, 0),
(2, 0, 1, 1),
(2, 0, 2, 0),
(2, 0, 2, 1),
(2, 0, 3, 0),
(2, 0, 3, 1),
(2, 1, 0, 0),
(2, 1, 0, 1),
(2, 1, 1, 0),
(2, 1, 1, 1),
(2, 1, 2, 0),
(2, 1, 2, 1),
(2, 1, 3, 0),
(2, 1, 3, 1),
(3, 0, 0, 0),
(3, 0, 0, 1),
(3, 0, 1, 0),
(3, 0, 1, 1),
(3, 0, 2, 0),
(3, 0, 2, 1),
(3, 0, 3, 0),
(3, 0, 3, 1),
(3, 1, 0, 0),
(3, 1, 0, 1),
(3, 1, 1, 0),
(3, 1, 1, 1),
(3, 1, 2, 0),
(3, 1, 2, 1),
(3, 1, 3, 0),
(3, 1, 3, 1),
(4, 0, 0, 0),
(4, 0, 0, 1),
(4, 0, 1, 0),
(4, 0, 1, 1),
(4, 0, 2, 0),
(4, 0, 2, 1),
(4, 0, 3, 0),
(4, 0, 3, 1),
(4, 1, 0, 0),
(4, 1, 0, 1),
(4, 1, 1, 0),
(4, 1, 1, 1),
(4, 1, 2, 0),
(4, 1, 2, 1),
(4, 1, 3, 0),
(4, 1, 3, 1),
(5, 0, 0, 0),
(5, 0, 0, 1),
(5, 0, 1, 0),
(5, 0, 1, 1),
(5, 0, 2, 0),
(5, 0, 2, 1),
(5, 0, 3, 0),
(5, 0, 3, 1),
(5, 1, 0, 0),
(5, 1, 0, 1),
(5, 1, 1, 0),
(5, 1, 1, 1),
(5, 1, 2, 0),
(5, 1, 2, 1),
(5, 1, 3, 0),
(5, 1, 3, 1)]
which is really the output that I want. I just need to modify the code to accept divPair.values() in some way. So I write this:
from itertools import product
divPair = div_pair(84000)
list(product(range(i+1) for i in divPair.values()))
which seems to me as if it should be correct, but it outputs this mess:
[(range(0, 6),), (range(0, 2),), (range(0, 4),), (range(0, 2),)]
and I can't figure out how to fix it. There is a post here which offers fantastic solutions to what I am trying to do. I am just trying to work toward them with what I know.
product returns the product of its arguments, and you have passed it a single one, the (range(i+1) for i in divPair.values()) generator. The generator yielded a list of range objects. That's like doing this:
>>> list(product(['range', 'range', 'range']))
[('range',), ('range',), ('range',)]
You have to pass your ranges as individual arguments.
Do this:
list(product(*[range(i+1) for i in divPair.values()]))
(or this)
list(product(*(range(i+1) for i in divPair.values())))