Reorder one nested list to match another - geometry

I have a nested list:
coords = [[4, 4], [5, 4], [6, 4], [6, 5], [6, 6], [5, 6], [4, 6], [4, 5]]
After applying a process to this list one of the items is changed (for example (6, 6) ---> (7, 6))
However in the process the order of the items has shifted and creates a new list (cstates)
cstates = [[4, 4], [4, 5], [4, 6], [5, 4], [5, 6], [6, 4], [6, 5], [7, 6]]
Is it possible to reorder cstates such that it matches the order of coords and the item which was changed is inserted into the remaining position?
Ps the order important as it draws out a shape.
enter image description here
enter image description here
I was hoping there was a predefined function that could do this, rather than having to iterate through each item

You can store additional item - index in initial list, and after transformation restore order using sort by this parameter
[[4, 4, 0], [5, 4, 1], [6, 4, 2], ...

Related

Element wise addition of two n dimensional lists

When I need to add two 2D lists element wise, the approach I am using is
l1 = [[1, 1, 1],
[2, 2, 2],
[3, 3, 3]]
l2 = [[1, 1, 1],
[2, 2, 2],
[3, 3, 3]]
new = list(map(lambda e: [sum(x) for x in zip(*e)], zip(l1, l2)))
print(new)
output : [[2, 2, 2],
[4, 4, 4],
[6, 6, 6]]
This code is already difficult to read.
So how would I add two n dimensional lists element wise? Is there a pythonic way to do it or should I use numpy?

print values from a list in vertical order for many lists

I'm having trouble printing data from a list vertically , the list is as shown below
[[1, 4, 5], [4, 6, 8], [8, 3, 10]]
I want to print the data into a new list as follows:
[[1, 4, 8], [4, 6, 3], [5, 8, 10]]
I'm having trouble doing it when the lists get longer, as it is a nested list
If the data is only numbers / integers, then you might want to use numpy for this. It will be faster too.
import numpy
givenList = [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
toNumpy = numpy.array(givenList) #convert to numpy array
toNumpy = toNumpy.T #transpose
toList = toNumpy.tolist() #convert back to list
print(toList)
# output : [[1, 4, 8], [4, 6, 3], [5, 8, 10]]
I think you are looking for zip.
l = [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
z = zip(*l)
print('\n'.join(map(str, z)))
# Output is:
# (1, 4, 8)
# (4, 6, 3)
# (5, 8, 10)
It does produce tuples instead of lists, but that is usually easily dealt with, and if you are just iterating over them, then it probably doesn't matter.
l = [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
z = map(list, zip(*l))
print('\n'.join(map(str, z)))
Will give you the same result, but will print them out as lists.

Creating Kfold cross validation set without sklearn

I am trying to split my data into K-folds with train and test set. I am stuck at the end:
I have a data set example:
[1,2,3,4,5,6,7,8,9,10]
I have successful created the partition for 5-fold cross validation and the output is
fold=[[2, 1], [6, 0], [7, 8], [9, 5], [4, 3]]
Now I want to create K such instances having K-1 training data and 1 validation set.
I am using this code:
```
cross_val={"train":[],"test":[]}
new_fold=folds.copy()
for i in range(4):
val=folds.pop(i)
cross_val["train"].append(folds)
cross_val["test"].append(val)
folds[i:i]=[val]```
The output that I am getting is:
{'train': [[[6, 0], [7, 8], [9, 5], [4, 3]],
[[6, 0], [7, 8], [9, 5], [4, 3]],
[[6, 0], [7, 8], [9, 5], [4, 3]],
[[6, 0], [7, 8], [9, 5], [4, 3]]],
'test': [[6, 0], [7, 8], [9, 5], [4, 3]]}
This is the wrong output that I am getting.
But I want the output as
train test
[[6, 0], [7, 8], [9, 5], [4, 3]] [2,1]
[[2, 1], [7, 8], [9, 5], [4, 3]] [6,0]
[[6, 0], [2, 1], [9, 5], [4, 3]] [7,8]
[[6, 0], [7, 8], [9, 5], [2, 1]] [4,3]
[[6, 0], [7, 8], [2, 1], [4, 3]] [9,5]
You here each time make edits to the same list, and append that list multiple times. As a result if you edit the list, you see that edit in all elements of the list.
You can create a cross-fold validation with:
train = []
test = []
cross_val={'train': train, 'test': test}
for i, testi in enumerate(fold):
train.append(fold[:i] + fold[i+1:])
test.append(testi)
For the given sample data, this gives us:
>>> pprint(cross_val)
{'test': [[2, 1], [6, 0], [7, 8], [9, 5], [4, 3]],
'train': [[[6, 0], [7, 8], [9, 5], [4, 3]],
[[2, 1], [7, 8], [9, 5], [4, 3]],
[[2, 1], [6, 0], [9, 5], [4, 3]],
[[2, 1], [6, 0], [7, 8], [4, 3]],
[[2, 1], [6, 0], [7, 8], [9, 5]]]}

Removing a list from a list of lists! PYTHON

a1=[[1, 2], [2, 3], [2, 4],[3, 4] ,[3, 6], [4, 5]]
i want the output to be:
a1=[[1, 2], [2, 3], [3, 4], [4, 5]]
I've tried removing it with a for loop, but it throws an error index out of range
You can use pop() if you want to remove by index (e.g. the fourth element):
In [1]: a1 = [[1, 2], [2, 3], [2, 4],[3, 4] ,[3, 6], [4, 5]]
In [2]: a1.pop(4)
Out[2]: [3, 6]
In [3]: a1
Out[3]: [[1, 2], [2, 3], [2, 4], [3, 4], [4, 5]]
Or, you can remove by specifying the element:
In [4]: a1 = [[1, 2], [2, 3], [2, 4],[3, 4] ,[3, 6], [4, 5]]
In [5]: a1.remove([3, 6])
In [6]: a1
Out[6]: [[1, 2], [2, 3], [2, 4], [3, 4], [4, 5]]
The answer is very simple just use the pop function.
https://www.geeksforgeeks.org/python-list-pop/
For your case it would be :
a1.pop(4)
you can loop over the Pop() function to remove multiple ones.

extract elements of a list and putting elements in another list, if I want to have the first pairs of a common second pair in general

In python,How can I extract elements of a list and putting elements in another list, if I want to have the first pairs of a common second pair in general, so the first pairs can be a lists that have 1 pair or more and the second pairs must be only 1 pair
for example, if I have below list:
X=[ [[1, 6], [1, 6]], [[1, 6], [1, 7]], [[2, 7], [1, 7]], [[2, 7], [2, 7]], [[2, 4], [1, 4]], [[2, 4], [1, 5]], [[2, 4], [1, 6]]]
I want this output in general not only for this example:
X1=[ [[[1,6],[2,4]],[1,6]] , [[[1,6],[2,7]],[1,7]] , [[2,7],[2,7]] , [[2,4],[1,4]] , [[2,4],[1,5]] ]

Resources