Break the sublists with Python - python

Here's the list I want to break:
List A = [[[[0, 1], [2, 3]], [[0, 2], [1, 3]], [[0, 3], [1, 2]]], [[[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]]]]
List A has 2 sublists, each of which contains 3 pairs of coordinates. I'm wondering if I could keep the order of those coordinates, but regroup a pair of coordinate as a sublist. So here's the desired output:
List B = [[[0, 1], [2, 3]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]]]
Thanks!!

You can convert it to a numpy array, reshape it, and then convert it back.
import numpy as np
A = [[[[0, 1], [2, 3]], [[0, 2], [1, 3]], [[0, 3], [1, 2]]], [[[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]]]]
npA = np.array(A)
B = npA.reshape(6, 2, 2).tolist()
Or, if you want it to generalize to different input sizes
B = npA.reshape(npA.size // 4, 2, 2).tolist()

As for your specific question, we can get B from A[0]+A[1]
>>> A = [[[[0, 1], [2, 3]], [[0, 2], [1, 3]], [[0, 3], [1, 2]]], [[[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]]]]
>>> B = [[[0, 1], [2, 3]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]]]
>>> A[0] + A[1]
[[[0, 1], [2, 3]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]], [[0, 3], [1, 2]], [[0, 2], [1, 3]]]
>>> A[0] + A[1] == B
True

For your specific requirement, You can take the first element of the list A and extend it with the second element of the list A
B = A[0]
B.extend(A[1])

Related

List all combinations of 3 equally sized partitions of a list

I have a vector containing 15 values and would like to find all the possible ways to partition the vector into 3 equally sized partitions. I know there is n!/(n-r)!r! combinations to take r values out of a list of n values with replacement & this is easily generated with itertools in Python.
Does there exist an easy solution to list all combinations in this case as well?
Mathematically there will be n!/((n/3!)^3)/3! solutions, for example if n=15 there will be 126126 combinations and if n=6 there will be 15 combinations.
As the task needs to remove duplicates, which is not supported by itertools, I would recommend package more_itertools:
import more_itertools
n = 6
assert n%3 == 0
[x for x in more_itertools.set_partitions(range(n), 3) if len(x[0]) == len(x[1]) == len(x[2])]
[[[0, 1], [2, 3], [4, 5]],
[[0, 1], [3, 4], [2, 5]],
[[0, 1], [2, 4], [3, 5]],
[[1, 2], [0, 3], [4, 5]],
[[0, 2], [1, 3], [4, 5]],
[[1, 2], [3, 4], [0, 5]],
[[0, 2], [3, 4], [1, 5]],
[[1, 2], [0, 4], [3, 5]],
[[0, 2], [1, 4], [3, 5]],
[[2, 3], [1, 4], [0, 5]],
[[2, 3], [0, 4], [1, 5]],
[[1, 3], [2, 4], [0, 5]],
[[0, 3], [2, 4], [1, 5]],
[[1, 3], [0, 4], [2, 5]],
[[0, 3], [1, 4], [2, 5]]]

cant store the value in global variable

Im trying to do permutation problem in leetcode with backtracking Algorithm, While Printing I got all the Possiblities but when i Trying to store those value in global variable I'm not allow to that
Ex:
AnswerIwant:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
when i print those value while recursion it is print
Example:
[1, 2, 3]
[1, 3, 2]
[2, 3, 1]
[2, 1, 3]
[3, 1, 2]
[3, 2, 1]
When I store The Value In Global variable for outPut I got like This
[1, 2, 3]
[[1, 2, 3]]
[[1, 2]]
[1, 3, 2]
[[1, 3, 2], [1, 3, 2]]
[[1, 3], [1, 3]]
[[1], [1]]
[2, 3, 1]
[[2, 3, 1], [2, 3, 1], [2, 3, 1]]
[[2, 3], [2, 3], [2, 3]]
[2, 1, 3]
[[2, 1, 3], [2, 1, 3], [2, 1, 3], [2, 1, 3]]
[[2, 1], [2, 1], [2, 1], [2, 1]]
[[2], [2], [2], [2]]
[3, 1, 2]
[[3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2], [3, 1, 2]]
[[3, 1], [3, 1], [3, 1], [3, 1], [3, 1]]
[3, 2, 1]
[[3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1], [3, 2, 1]]
[[3, 2], [3, 2], [3, 2], [3, 2], [3, 2], [3, 2]]
[[3], [3], [3], [3], [3], [3]]
[[], [], [], [], [], []]
Here Is my code For the Above OutPut
nums = [1,2,3]
val=nums
answerIwant = []
numberOfValues=len(val)-1
answer=[]
def permutation(nums, bucket, index,sub):
global answerIwant,numberOfValues
for i in range(len(nums)):
val=nums.pop(0)
bucket.append(val)
if len(bucket)-1==numberOfValues:
print(bucket)
answerIwant.append(bucket)
permutation(nums, bucket, index,sub)
nev=bucket.pop()
nums.append(nev)
print(answerIwant)
#[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
sub=[]
bucket = []
val=(permutation(nums, bucket, index,sub))
print(val)
Explain that problem i'm facing with symbol word
Thank You
You have to append the copy of the list during appening in the answerIwant list. So you should replace answerIwant.append(bucket) to answerIwant.append(bucket.copy()).
Refer this for more info. So your code could be this,
nums = [1,2,3]
val=nums
answerIwant = []
numberOfValues=len(val)-1
answer=[]
def permutation(nums, bucket, index,sub):
global numberOfValues
for i in range(len(nums)):
val=nums.pop(0)
bucket.append(val)
if len(bucket)-1==numberOfValues:
# print(bucket)
answerIwant.append(bucket.copy())
permutation(nums, bucket, index,sub)
nev = bucket
nev=bucket.pop()
nums.append(nev)
#[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
sub=[]
bucket = []
permutation(nums, bucket, 0,sub)
print(answerIwant)

how to split a list in python based on the values of the list

I have a list having sublists of numbers and want to extract specific ones. In my simplified example I have two main sublists and each one has its own pairs of numbers:
data=[[[1, 0], [2, 0], [2, 1], [2, 2],\
[1, 0], [1, 1], [1, 2],\
[0, 1], [0, 2], [0, 3]],\
[[1, 0], [2, 0],\
[1, 0],\
[0, 1], [0, 2], [1, 2],\
[1, 0], [1, 1], [1, 1]]]
Pairs stored in data can be divided based on some rules and I want the last pair of each division. For simplicity I have shown each division as a row in data. Each division starts with [1, 0] or [0, 1] and these two pairs are break points. Then, simply I want the last pair before each break points. In cases I may have no point between two break points and I only export the previous break point. Finally I want it as the following list:
data=[[[2, 2],\
[1, 2],\
[0, 3]],\
[[2, 0],\
[1, 0],\
[1, 2],\
[1, 1]]]
You can do the following, using enumerate:
def fun(lst):
return [p for i, p in enumerate(lst) if i==len(lst)-1 or set(lst[i+1])=={0,1}]
[*map(fun, data)]
# [[[2, 2], [1, 2], [0, 3]], [[2, 0], [1, 0], [1, 2], [1, 1]]]
fun filters a nested list for all elements that are either last or succeeded by [0, 1] or [1, 0].
data=[[[1, 0], [2, 0], [2, 1], [2, 2],
[1, 0], [1, 1], [1, 2],
[0, 1], [0, 2], [0, 3]],
[[1, 0], [2, 0],
[1, 0],
[0, 1], [0, 2], [1, 2],
[1, 0], [1, 1], [1, 1]]]
newData = []
for subarray in data:
new_subarray = []
for i,item in enumerate(subarray):
if item == [0,1] or item == [1,0]:
if i> 0:
new_subarray.append(subarray[i-1])
if i == len(subarray)-1:
new_subarray.append(item)
newData.append(new_subarray)
print(newData)
Here is a fun little unreadable numpy oneliner:
import numpy as np
[np.array(a)[np.roll(np.flatnonzero(np.logical_or(np.all(np.array(a)==(1, 0), axis=1), np.all(np.array(a)==(0, 1), axis=1)))-1, -1)].tolist() for a in data]
# [[[2, 2], [1, 2], [0, 3]], [[2, 0], [1, 0], [1, 2], [1, 1]]]
It works but in reality you'd better use schwobaseggl's solution.

Sorting a list of list

So I have a list like below:
points = [[0, 0], [5, 3], [0, 5], [0, 2], [1, 3], [5, 3]]
I have been using
points.sort(key=lambda pair: pair[0])
to sort the list. But this only sorts it by first value without looking at the second value.
The result of this code is as below:
[[0, 0], [0, 5], [0, 2], [1, 3], [5, 3], [5, 3]]
However, I want to sort is such that the result is:
[[0, 0], [0, 2], [0, 5], [1, 3], [5, 3], [5, 3]]
How do I do it?
Also, while we are at it, how do i remove the duplicates from this list? The final result should be like:
[[0, 0], [0, 2], [0, 5], [1, 3], [5, 3]]
Python already sorts iterable containers (tuples, lists, ...) and strings lexicographically.
>>> points = [[0, 0], [5, 3], [0, 5], [0, 2], [1, 3], [5, 3]]
>>> sorted(points)
[[0, 0], [0, 2], [0, 5], [1, 3], [5, 3], [5, 3]]
import itertools
points = [[0, 0], [5, 3], [0, 5], [0, 2], [1, 3], [5, 3]]
points.sort()
nList = list(points for points,_ in itertools.groupby(points))
print nList
Result:
[[0, 0], [0, 2], [0, 5], [1, 3], [5, 3]]
You can have a sort key that first sorts by the first element x[0], and if their are ties, sort by the second element x[1]:
>>> points = [[0, 0], [5, 3], [0, 5], [0, 2], [1, 3], [5, 3], [0, 4]]
>>> sorted(points, key = lambda x: (x[0], x[1]))
[[0, 0], [0, 2], [0, 4], [0, 5], [1, 3], [5, 3], [5, 3]]
If you don't want any duplicates in this final list, then you can do this:
points = [[0, 0], [5, 3], [0, 5], [0, 2], [1, 3], [5, 3]]
sorted_lst = sorted(points, key = lambda x: (x[0], x[1]))
seen = set()
no_dups = []
for lst in sorted_lst:
curr = tuple(lst)
if curr not in seen:
no_dups.append(lst)
seen.add(curr)
print(no_dups)
# [[0, 0], [0, 2], [0, 5], [1, 3], [5, 3]]
Which has a set seen which keeps track of what sublists have been added, and a list no_dups where the lists get added. If an element is not in seen and add it to the final list, and add it to seen, which indicates that the element has already been added.
Also since type list is not a hashable type, you cannot add them directly to a set. In the above code, the lists are converted to tuples and added to seen, which is a hashable type.
Try itemgetter
from operator import itemgetter
sorted(points, key=itemgetter(0, 1))

How to flatten a list in python?

I have a program which outputs solutions, which are lists of numbers as such:
[[1, [[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]], [2, [[1, 4], [2, 3], [3, 2], [4, 1]]], [3, [[1, 3], [2, 2], [3, 1]]], [4, [[1, 2], [2, 1]]], [5, [[1, 1]]]]
What I want this to be turned into is:
[[1,1,5],[1,2,4],[1,3,3],[1,4,2],[1,5,1],[2,1,4],[2,2,3],[2,3,2],[2,4,1],[3,1,3],[3,2,2],[3,3,1],[4,1,2],[4,2,1],[5,1,1]]
Basically, each term in the whole list is headed by a number, which forms the first item of all possible solutions with that number, and then the following lists indicate what is to be added.
This should work. It works with the example that you have atleast.
result = [[[i[0]] + j for j in i[1:][0] ] for i in arr][0]
items = [[1, [[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]], [2, [[1, 4], [2, 3], [3, 2], [4, 1]]], [3, [[1, 3], [2, 2], [3, 1]]], [4, [[1, 2], [2, 1]]], [5, [[1, 1]]]]
flat_items = []
for item in items:
leading = item[0]
for i in item[1]:
flat_items.append([leading]+i)
print(flat_items)
Yet another solution that gives you an output as you needed:
def flatten(lst):
result = []
for i in lst:
for j in i[1]:
pair = j[:]
pair.insert(0, i[0])
result.append(pair)
return result
Introduce two functions: merge and flatten.
The second function is taken from here:
def flatten(lst):
return [item for sublist in lst for item in sublist]
The first function is defined as:
def merge(lst):
return [[lst[0]] + x for x in flatten(lst[1:])]
Then call them:
s = [[1, [[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]], [2, [[1, 4], [2, 3], [3, 2], [4, 1]]], [3, [[1, 3], [2, 2], [3, 1]]], [4, [[1, 2], [2, 1]]], [5, [[1, 1]]]]
print flatten([merge(x) for x in s])
Output is:
[[1, 1, 5], [1, 2, 4], [1, 3, 3], [1, 4, 2], [1, 5, 1], [2, 1, 4], [2, 2, 3], [2, 3, 2], [2, 4, 1], [3, 1, 3], [3, 2, 2], [3, 3, 1], [4, 1, 2], [4, 2, 1], [5, 1, 1]]

Categories

Resources