how to make pythonic Nested List - python

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

Related

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

Sorting a list of multi-dimensional points

I have a list of D-dimensional points (where D is a constant), and I would like to sort them first based on the 1st dimension values, then by the 2nd dimesion values and so on, so if 2 points have the same values at the first x dimensions, they would be sorted based on the values of dimension x+1.
I know that if my number of dimension is final, I could use this solution: https://stackoverflow.com/a/37111840
But since I have D dimensions where D is a constant number in the code, I'm not sure how to define the sorting "key" values well.
As #iz_ points out, this is how python sorting works by default. Here is an example illustrating this point:
import itertools
import random
# generate all length 3 tuples of 0s 1s and 2s
foo = list(itertools.product(*([range(3)]*3)))
#mix them all up
random.shuffle(foo)
print(foo)
# this sorts by the first, then the second, then the last
foo.sort()
print(foo)
[(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)]
example list l1 with three dimensions:
l1 = [[0, 3, 0], [0, 0, 3], [1, 0, 3], [2, 2, 1], [2, 1, 1], [2, 1, 0], [1, 0, 0], [2, 0, 0], [1, 1, 2], [1, 2, 3]]
l2 = sorted(l1, key = lambda k: k[::-1])
print(l2)
[[1, 0, 0], [2, 0, 0], [2, 1, 0], [0, 3, 0], [2, 1, 1], [2, 2, 1], [1, 1, 2], [0, 0, 3], [1, 0, 3], [1, 2, 3]]
I'm not sure whether that's exactly the sort order that you wanted, but it keys on k values of arbitrary length (as long as all elements of l1 are the same length).

List every permutations of iterable [duplicate]

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

python: perform a generic multi dimensional loop

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)

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

Categories

Resources