find combinations in arbitrarily nested lists under conditions - python

I want to find possible paths on a finite grid of points. Say, starting point is (x,y). Then next point (m,n) in the path is given by conditions
(m!=x) and (n!=y) ie. I exclude the row and column I was in previously.
n < y ie. I always hop DOWN.
m,n >= 0 ie. all the points are always in first quadrant
Stopping criteria is when a point lies on x axis.
Hence, generate all possible combinations of such 'paths' possible.
Following is what I've tried.
def lisy(x,y):
return [(i,j) for i in range(4,0,-1) for j in range(4,0,-1) if(i!=x and j<y)]
def recurse(x,y):
if (not lisy(x,y)):
return (x,y)
else:
return [(x,y), [recurse(i,j) for i,j in lisy(x,y)]]
OUTPUT:
In [89]: recurse(1,4)
Out[89]:
[(1, 4),
[[(4, 3),
[[(3, 2), [(4, 1), (2, 1), (1, 1)]],
(3, 1),
[(2, 2), [(4, 1), (3, 1), (1, 1)]],
(2, 1),
[(1, 2), [(4, 1), (3, 1), (2, 1)]],
(1, 1)]],
[(4, 2), [(3, 1), (2, 1), (1, 1)]],
(4, 1),
[(3, 3),
[[(4, 2), [(3, 1), (2, 1), (1, 1)]],
(4, 1),
[(2, 2), [(4, 1), (3, 1), (1, 1)]],
(2, 1),
[(1, 2), [(4, 1), (3, 1), (2, 1)]],
(1, 1)]],
[(3, 2), [(4, 1), (2, 1), (1, 1)]],
(3, 1),
[(2, 3),
[[(4, 2), [(3, 1), (2, 1), (1, 1)]],
(4, 1),
[(3, 2), [(4, 1), (2, 1), (1, 1)]],
(3, 1),
[(1, 2), [(4, 1), (3, 1), (2, 1)]],
(1, 1)]],
[(2, 2), [(4, 1), (3, 1), (1, 1)]],
(2, 1)]]
This gives me a nested lists of possible new points from each point.
Can anyone tell me how to process my list obtained from recurse(1,4)?
edit1:
Effectively I hop from a given starting point (in a 4x4 grid [finite]), satisfying the three conditions mentioned until stopping criteria is met, ie. m,n > 0

I clarify the requirements I am working under in the docstring of my generator gridpaths(). Note that I have the horizontal size of the grid as a global variable and the vertical size of the grid is irrelevant, the x-coordinates of path points can be up to but not exceed that global value, and x-coordinates of non-consecutive path points can be equal (though consecutive path points must have different x-coordinates). I changed the name of the routine but kept the arguments as you had them. This version of my code adds the requirement that the y-coordinate of the final point on the path must be 1, and it also is safer in accepting arguments.
This is a generator of lists, so my test code shows how large the generator is then prints all the lists.
def gridpaths(x, y):
"""Generate all paths starting at (x,y) [x and y must be positive
integers] where, if (m,n) is the next point in the path after
(x,y), then m and n are positive integers, m <= xsize [xsize is a
global variable], m != x, and n < y, and so on for all consecutive
path points. The final point in the path must have a y-coordinate
of 1. Paths are yielded in lexicographic order."""
def allgridpaths(x, y, pathsofar):
"""Generate all such paths continuing from pathssofar without
the y == 1 requirement for the final path point."""
newpath = pathsofar + [(x, y)]
yield newpath
for m in range(1, xsize+1):
if m != x:
for n in range(1, y):
for path in allgridpaths(m, n, newpath):
yield path
x, y = max(int(x), 1), max(int(y), 1) # force positive integers
for path in allgridpaths(x, y, []):
# Only yield paths that end at y == 1
if path[-1][1] == 1:
yield path
# global variable: horizontal size of grid
xsize = 4
print(sum(1 for p in gridpaths(1, 4)), 'paths total.')
for p in gridpaths(1, 4):
print(p)
The printout shows that the point (1,4) in a 4x4 grid yields 48 paths. In fact, gridpaths(x, y) will return (xsize - 1) * xsize ** (y - 2) paths, which can grow very quickly. That is why I programmed a generator of lists rather than a list of lists. Let me know if your requirements are different from what I suppose. The printout from that code above is:
48 paths total.
[(1, 4), (2, 1)]
[(1, 4), (2, 2), (1, 1)]
[(1, 4), (2, 2), (3, 1)]
[(1, 4), (2, 2), (4, 1)]
[(1, 4), (2, 3), (1, 1)]
[(1, 4), (2, 3), (1, 2), (2, 1)]
[(1, 4), (2, 3), (1, 2), (3, 1)]
[(1, 4), (2, 3), (1, 2), (4, 1)]
[(1, 4), (2, 3), (3, 1)]
[(1, 4), (2, 3), (3, 2), (1, 1)]
[(1, 4), (2, 3), (3, 2), (2, 1)]
[(1, 4), (2, 3), (3, 2), (4, 1)]
[(1, 4), (2, 3), (4, 1)]
[(1, 4), (2, 3), (4, 2), (1, 1)]
[(1, 4), (2, 3), (4, 2), (2, 1)]
[(1, 4), (2, 3), (4, 2), (3, 1)]
[(1, 4), (3, 1)]
[(1, 4), (3, 2), (1, 1)]
[(1, 4), (3, 2), (2, 1)]
[(1, 4), (3, 2), (4, 1)]
[(1, 4), (3, 3), (1, 1)]
[(1, 4), (3, 3), (1, 2), (2, 1)]
[(1, 4), (3, 3), (1, 2), (3, 1)]
[(1, 4), (3, 3), (1, 2), (4, 1)]
[(1, 4), (3, 3), (2, 1)]
[(1, 4), (3, 3), (2, 2), (1, 1)]
[(1, 4), (3, 3), (2, 2), (3, 1)]
[(1, 4), (3, 3), (2, 2), (4, 1)]
[(1, 4), (3, 3), (4, 1)]
[(1, 4), (3, 3), (4, 2), (1, 1)]
[(1, 4), (3, 3), (4, 2), (2, 1)]
[(1, 4), (3, 3), (4, 2), (3, 1)]
[(1, 4), (4, 1)]
[(1, 4), (4, 2), (1, 1)]
[(1, 4), (4, 2), (2, 1)]
[(1, 4), (4, 2), (3, 1)]
[(1, 4), (4, 3), (1, 1)]
[(1, 4), (4, 3), (1, 2), (2, 1)]
[(1, 4), (4, 3), (1, 2), (3, 1)]
[(1, 4), (4, 3), (1, 2), (4, 1)]
[(1, 4), (4, 3), (2, 1)]
[(1, 4), (4, 3), (2, 2), (1, 1)]
[(1, 4), (4, 3), (2, 2), (3, 1)]
[(1, 4), (4, 3), (2, 2), (4, 1)]
[(1, 4), (4, 3), (3, 1)]
[(1, 4), (4, 3), (3, 2), (1, 1)]
[(1, 4), (4, 3), (3, 2), (2, 1)]
[(1, 4), (4, 3), (3, 2), (4, 1)]

Related

How to select spesific list from nested lists with python [duplicate]

This question already has answers here:
Pythonic way of checking if a condition holds for any element of a list
(3 answers)
Apply function to each element of a list
(4 answers)
Closed 9 months ago.
Hello guys I have list like A
max_x=4
min_x=0
A=[[(0, 1), (1, 0), (1, 1), (2, 0)], [(0, 3), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 0), (4, 1), (4, 2)]]
A, includes different group of points (x,y) format.I wanted to find group if includes my max and min same time.Output should be like B.Because this cluster includes 0 and 4 as x.
B= [(0, 3), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 0), (4, 1), (4, 2)]
Thank you.
You could use a list comprehension to find any sublists of A that have a tuple that has x == min_x and also a tuple that has x == max_x:
max_x=4
min_x=0
A=[[(0, 1), (1, 0), (1, 1), (2, 0)], [(0, 3), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 0), (4, 1), (4, 2)]]
B = [l for l in A if any(x == min_x for x,_ in l) and any(x == max_x for x,_ in l)]
Output:
[[(0, 3), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 0), (4, 1), (4, 2)]]

Iterating through a list/dictionary over and over again with a timestep value

So I have a dictionary:
{0: [[(4, 1), (1, 4)], [(2, 3), (3, 2)], [(4, 2), (2, 4), (1, 3), (3, 1)], [(1, 2), (2, 1), (4, 3), (3, 4)]]}
How do I iterate through the nested list values [(4, 1), (1, 4)], [(2, 3), (3, 2)]... over and over again depending on a initiated count value?
For example: initiated_count_value = 0 would refer to [(4, 1), (1, 4)], while initiated_count_value = 3 would refer to [(1, 2), (2, 1), (4, 3), (3, 4)] but then as the count changes by going up the values of the nested list are still referenced.
For example: initiated count value of 4 would refer back again to [(4, 1), (1, 4)].
Thanks.
To loop the overall list index back to the beginning, you can use the % modulo operator.
d = {
0: [
[(4, 1), (1, 4)],
[(2, 3), (3, 2)],
[(4, 2), (2, 4), (1, 3), (3, 1)],
[(1, 2), (2, 1), (4, 3), (3, 4)],
]
}
for initiated_count_value in range(6):
di = initiated_count_value % len(d[0])
print(d[0][di])
--
[(4, 1), (1, 4)]
[(2, 3), (3, 2)]
[(4, 2), (2, 4), (1, 3), (3, 1)]
[(1, 2), (2, 1), (4, 3), (3, 4)]
[(4, 1), (1, 4)]
[(2, 3), (3, 2)]
You could use a generator which took an initial index and then iterated through the list starting at that index and wrapping around:
def get_list(icv):
l = len(dct[0])
while True:
yield dct[0][icv % l]
icv += 1
You could then do something like:
initiated_count_value = 3
for l in get_list(initiated_count_value):
print(l)
Output:
[(1, 2), (2, 1), (4, 3), (3, 4)]
[(4, 1), (1, 4)]
[(2, 3), (3, 2)]
[(4, 2), (2, 4), (1, 3), (3, 1)]
[(1, 2), (2, 1), (4, 3), (3, 4)]
[(4, 1), (1, 4)]
[(2, 3), (3, 2)]
[(4, 2), (2, 4), (1, 3), (3, 1)]
...
If you don't want the loop to run forever, replace the while True in get_list with something like a for i in range(100) or similar.

How to get all possible unique variants from all possible combinations

I have a list of points
lst = [1,2,3,4,5]
With itertools I get all possible combinations of lines ((1,2)=(2,1)): itertools.combinations(lst, 2)
[(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]
I want to get list of lists of tuples(with unique points), like this
[
[(1,2),(3,4)],
[(1,2),(3,5)],
[(1,2),(4,5)],
[(1,3),(2,4)],
[(1,3),(2,5)],
...
[(2,3),(4,5)]
]
Ok, this is interesting xD. All you need is provided by itertools, you just have to combine it the correct way. Have a look at this:
import itertools
lst = [1,2,3,4,5]
points = list(itertools.combinations(lst, 2))
# [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)]
f = lambda p: filter(lambda sub: not any(x in sub for x in p), points)
res = []
for p in points:
res.extend(list(itertools.product([p], f(p))))
# corresponding list-comprehension solution
# res = list(itertools.chain.from_iterable(itertools.product([p], f(p)) for p in points))
which returns:
res = [((1, 2), (3, 4)), ((1, 2), (3, 5)), ((1, 2), (4, 5)), ((1, 3), (2, 4)), ((1, 3), (2, 5)), ((1, 3), (4, 5)), ((1, 4), (2, 3)), ((1, 4), (2, 5)), ((1, 4), (3, 5)), ((1, 5), (2, 3)), ((1, 5), (2, 4)), ((1, 5), (3, 4)), ((2, 3), (1, 4)), ((2, 3), (1, 5)), ((2, 3), (4, 5)), ((2, 4), (1, 3)), ((2, 4), (1, 5)), ((2, 4), (3, 5)), ((2, 5), (1, 3)), ((2, 5), (1, 4)), ((2, 5), (3, 4)), ((3, 4), (1, 2)), ((3, 4), (1, 5)), ((3, 4), (2, 5)), ((3, 5), (1, 2)), ((3, 5), (1, 4)), ((3, 5), (2, 4)), ((4, 5), (1, 2)), ((4, 5), (1, 3)), ((4, 5), (2, 3))]
It basically boils down to itertools.combinations (which you already did) with itertools.product. The twist is the filtering performed between the two (see f = lambda ...). If you need further assistance understanding the code, let me know.

Re-group 2D coordinates of pairs in Python

How can I rearrange [(0, 4), (1, 3)] to (0, 1), (4, 3), in other words group first coordinates together and second coordinates together (order maintained left to right). I need to do this for a large list of, exampled below
c = [[(0, 4), (1, 3)],
[(0, 4), (1, 5)],
[(1, 3), (2, 2)],
[(1, 3), (2, 4)],
[(1, 5), (2, 2)],
[(1, 5), (2, 6)],
...]
So that the end result is
[[(0, 1), (4, 3)],
[(0, 1), (4, 5)],
[(1, 2), (3, 2)],
[(1, 2), (3, 4)],
[(1, 2), (5, 2)],
[(1, 2), (5, 6)],
...
]
How about
end_result = [[(w, y), (x, z)] for [(w, x), (y, z)] in c]
?

Making Combinations (Python)

In Python, is there a better way to get the set of combinations of n elements from a k-element set than nested for loops or list comprehensions?
For example, say from the set [1,2,3,4,5,6] I want to get [(1,2),(1,3),(1,4),(1,5),(1,6),(2,3),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6),(4,5),(4,6),(5,6)]. Is there a better of of making it than
nums=[1,2,3,4,5,6]
doubles=[]
for a in nums:
for b in nums[a+1:]
doubles.append((a,b))
? It's okay if the elements of the list we end up with are sets, tuples, or lists; I just feel there should be an easier way to do this.
You can use itertools.combinations:
>>> from itertools import combinations
>>> nums = [1,2,3,4,5,6]
>>> list(combinations(nums, 2))
[(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 3), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5), (4, 6), (5, 6)]
The itertools module has a lot of really powerful tools that can be used in situations like this. In this case, you want itertools.combinations. Some other ones that you might find useful are itertools.combinations_with_replacement and itertools.permutations.
Example:
>>> import itertools
>>> list(itertools.combinations(range(1,7),2))
[(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 3), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5), (4, 6), (5, 6)]
>>> list(itertools.combinations_with_replacement(range(1,7),2))
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (3, 3), (3, 4), (3, 5), (3, 6), (4, 4), (4, 5), (4, 6), (5, 5), (5, 6), (6, 6)]
>>> list(itertools.permutations(range(1,7),2))
[(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5)]
You could use the itertools module
import itertools
alphabet = ['1','2','3','4','5','6']
combos = list(itertools.combinations(alphabet, 2))
print combos

Categories

Resources