python: perform a generic multi dimensional loop - python

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)

Related

Create new list out of two lists via multiplication. Python

Good morning!
I'm trying to generate a new list out of two lists, by using multiplication operation.
Below I show you step by step what I did:
import itertools
from itertools import product
import numpy as np
import pandas as pd
Parameter_list=[]
Parameter=[range(0,2,1),range(0,2,1)]
Parameter_list=list(itertools.product(*Parameter))
print(Parameter_list)
[(0, 0), (0, 1), (1, 0), (1, 1)]
Then I deleted the first value, which is basically the null matrix:
del Parameter_list[0]
print(Parameter_list)
[(0, 1), (1, 0), (1, 1)]
I proceeded by creating the two paramter list:
Parameter_A=[range(1,2,1),range(3,6,2),range(10,20,10)]
Parameter_A=list(itertools.product(*Parameter_A))
Parameter_B=[range(0,2,1),range(4,6,2),range(10,20,10)]
Parameter_B=list(itertools.product(*Parameter_B))
print(Parameter_A)
print(Parameter_B)
[(1, 3, 10), (1, 5, 10)]
[(0, 4, 10), (1, 4, 10)]
And combined the lists:
comb=list(product(Parameter_A,Parameter_B))
print(comb)
[((1, 3, 10), (0, 4, 10)),
((1, 3, 10), (1, 4, 10)),
((1, 5, 10), (0, 4, 10)),
((1, 5, 10), (1, 4, 10))]
Until here no prob. But now I'm struggling to create a new list from multiplying the Parameter List with the comb list. The desired output is the following:
[((0, 0, 0), (0, 4, 10)),
((0, 0, 0), (1, 4, 10)),
((0, 0, 0), (0, 4, 10)),
((0, 0, 0), (1, 4, 10)),
((1, 3, 10), (0, 0, 0)),
((1, 3, 10), (0, 0, 0)),
((1, 5, 10), (0, 0, 0)),
((1, 5, 10), (0, 0, 0)),
((1, 3, 10), (0, 4, 10)),
((1, 3, 10), (1, 4, 10)),
((1, 5, 10), (0, 4, 10)),
((1, 5, 10), (1, 4, 10))]
Can someone help me? Many thanks!
Doing this with lists instead of with a numpy array is not the most convenient choice. That said, it's still something you can do with a one-liner.
prod = [tuple(i if j != 0 else (0,) * len(i) for i, j in zip(comb_items, bool_items))
for comb_items, bool_items in itertools.product(comb, Parameter_list)]
>>> prod
[((0, 0, 0), (0, 4, 10)),
((1, 3, 10), (0, 0, 0)),
((1, 3, 10), (0, 4, 10)),
((0, 0, 0), (1, 4, 10)),
((1, 3, 10), (0, 0, 0)),
((1, 3, 10), (1, 4, 10)),
((0, 0, 0), (0, 4, 10)),
((1, 5, 10), (0, 0, 0)),
((1, 5, 10), (0, 4, 10)),
((0, 0, 0), (1, 4, 10)),
((1, 5, 10), (0, 0, 0)),
((1, 5, 10), (1, 4, 10))]
I am assuming that the order of the outputs isn't critical and that the Parameter_list will always be booleans. Both of these things can be pretty easily changed if needed.

Reconstructing missing information from game results

I have a dataset of results from games of n players, where each game has n-1 players playing. The results of a game may look like this:
1 2 _
_ 1 2
2 _ 1
where each column represents the results of 1 player. However, the dataset has been corrupted and columns where players have a bye (_) have been collapsed so that results turn out like this:
1 2 2
2 1 1
I currently have python code to take in the results from a file and add them to an numpy array, which includes a function to insert a bye into a column. Printing the array gives this output:
[['1' '2' '2']
['1' '1' '2']
['0' '0' '0']]
I am struggling to figure out how to find the corrected results, especially if some collapsed results may have multiple solutions. I know I need to use a recursive solve () function, but I'm not sure how to go about it. Here is my current source code:
import numpy as np
collapsed_results = []
p = 0
def insert_bye(grid, row, column):
for i in reversed(range(row, p)):
if i == row:
grid[i][column] = "_"
else:
grid[i][column] = grid[i - 1][column]
return grid
def solve(collapsed_results):
pass
if __name__ == "__main__":
while True:
try:
line = input()
except EOFError:
break
line = line.split(" ")
collapsed_results.append(line)
# Number of players
p = len(collapsed_results[0])
collapsed_results.append([0] * p)
collapsed_results = np.array(collapsed_results)
You can use a recursive generator function:
from collections import deque
def pad_col(d, l, c=[]):
if len(c) == l:
yield c
else:
yield from ([] if not d else pad_col(d[1:], l, c+[d[0]]))
if l - len(c) > len(d):
yield from pad_col(d, l, c+[0])
def solve(collapsed, l = 3):
def combos(d, c = []):
if not d:
yield list(zip(*c))
else:
for i in pad_col(d[0], l):
yield from combos(d[1:], c+[i])
return list(combos([*zip(*collapsed)]))
print(solve([[1, 2, 2], [2, 1, 1]]))
Output:
[[(1, 2, 2), (2, 1, 1), (0, 0, 0)], [(1, 2, 2), (2, 1, 0), (0, 0, 1)], [(1, 2, 0), (2, 1, 2), (0, 0, 1)], [(1, 2, 2), (2, 0, 1), (0, 1, 0)], [(1, 2, 2), (2, 0, 0), (0, 1, 1)], [(1, 2, 0), (2, 0, 2), (0, 1, 1)], [(1, 0, 2), (2, 2, 1), (0, 1, 0)], [(1, 0, 2), (2, 2, 0), (0, 1, 1)], [(1, 0, 0), (2, 2, 2), (0, 1, 1)], [(1, 2, 2), (0, 1, 1), (2, 0, 0)], [(1, 2, 2), (0, 1, 0), (2, 0, 1)], [(1, 2, 0), (0, 1, 2), (2, 0, 1)], [(1, 2, 2), (0, 0, 1), (2, 1, 0)], [(1, 2, 2), (0, 0, 0), (2, 1, 1)], [(1, 2, 0), (0, 0, 2), (2, 1, 1)], [(1, 0, 2), (0, 2, 1), (2, 1, 0)], [(1, 0, 2), (0, 2, 0), (2, 1, 1)], [(1, 0, 0), (0, 2, 2), (2, 1, 1)], [(0, 2, 2), (1, 1, 1), (2, 0, 0)], [(0, 2, 2), (1, 1, 0), (2, 0, 1)], [(0, 2, 0), (1, 1, 2), (2, 0, 1)], [(0, 2, 2), (1, 0, 1), (2, 1, 0)], [(0, 2, 2), (1, 0, 0), (2, 1, 1)], [(0, 2, 0), (1, 0, 2), (2, 1, 1)], [(0, 0, 2), (1, 2, 1), (2, 1, 0)], [(0, 0, 2), (1, 2, 0), (2, 1, 1)], [(0, 0, 0), (1, 2, 2), (2, 1, 1)]]

Python. Find all possible combinations of numbers with set length

I have a list of numbers: [0, 0, 1, 1, 2, 2]
I want to have all combinations of 2 numbers, I tried to do it with itertools:
import itertools
a = [0, 0, 1, 1, 2, 2]
combinations = set(itertools.permutations(a, 2))
print(combinations)
# {(0, 1), (1, 2), (0, 0), (2, 1), (2, 0), (1, 1), (2, 2), (1, 0), (0, 2)}
I want to use all numbers in list to combinations, but itertools didn't do it.
So, I want to get result like this:
(0, 0), (0, 1), (0, 1), (1, 0), (1, 0) ...
So, since we have two 0 and two 1 we will have two (0, 1) combinations and etc.
A set is a data structure without duplicates. Use a list:
import itertools
a = [0, 0, 1, 1, 2, 2]
combinations = list(itertools.permutations(a, 2))
print(combinations)
Just use itertools.product, it gives all possible combinations:
from itertools import product
a = [0, 0, 1, 1, 2, 2]
print (list(product(a, repeat=2)))
gives:
[(0, 0), (0, 0), (0, 1), (0, 1),
(0, 2), (0, 2), (0, 0), (0, 0),
(0, 1), (0, 1), (0, 2), (0, 2),
(1, 0), (1, 0), (1, 1), (1, 1),
(1, 2), (1, 2), (1, 0), (1, 0),
(1, 1), (1, 1), (1, 2), (1, 2),
(2, 0), (2, 0), (2, 1), (2, 1),
(2, 2), (2, 2), (2, 0), (2, 0),
(2, 1), (2, 1), (2, 2), (2, 2)]

product of variable number of range(n)'s

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

Is there a way to add multiple conditions in a for loop?

n=int(raw_input('enter the number of mcnuggets you want to buy : ')) #total number of mcnuggets you want yo buy
for a in range(1,n) and b in range(1,n) and c in range(1,n) :
if (6*a+9*b+20*c==n):
print 'number of packs of 6 are ',a
print 'number of packs of 9 are ',b
print 'number of packs of 20 are',c
i am new to programming and i am learning python.the code above gives errors. Any suggestion.?.
You should use nested loops:
for a in range(1, n):
for b in range(1, n):
for c in range(1, n):
if ...
Or even better:
import itertools
for a, b, c in itertools.product(range(1, n + 1), repeat=3):
if ...
I think you should start the ranges from 0, otherwise you will only get answers that include at least one of each size. You can also make less work for the computer since you know that there will never be more than n/6 packs of 6 required etc. This can be a big saving - for 45 nuggets you only need to test 144 cases compared to 97336
from itertools import product
n=int(raw_input('enter the number of mcnuggets you want to buy : ')) #total number of mcnuggets you want to buy
for a,b,c in product(range(n//6+1), range(n//9+1), range(n//20+1)) :
if (6*a+9*b+20*c==n):
print 'number of packs of 6 are ',a
print 'number of packs of 9 are ',b
print 'number of packs of 20 are',c
itertools.product gives the cartesian product of the 3 ranges. For example
>>> from itertools import product
>>> list(product(range(3),range(4),range(5)))
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 0), (0, 2, 1), (0, 2, 2), (0, 2, 3), (0, 2, 4), (0, 3, 0), (0, 3, 1), (0, 3, 2), (0, 3, 3), (0, 3, 4), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 0, 3), (1, 0, 4), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 0), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 3, 0), (1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 0, 3), (2, 0, 4), (2, 1, 0), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 2, 0), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 0), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4)]
If you want to have values from multiple sequences in a for loop then you can use zip, for example:
for (a,b,c) in zip(xrange(1,n), xrange(1,n), xrange(1,n)) :
....
Of course it is a waste repeating the same range, but judging from the title of your post I guess that using the same range is only and example.

Categories

Resources