How to separate last column from 2D-array in Python? - python

I need to separate 2D array in order to have last column and the rest separately. Those must be arrays both.

Does this help?
>>> a = [[1,2,3], [4,5,6], [7,8,9]]
>>> l = [row[:-1] for row in a]
>>> r = [row[-1] for row in a]
>>> l
[[1, 2], [4, 5], [7, 8]]
>>> r
[3, 6, 9]

Related

Python: Appending numerous 2D list to one 2D list

For the sake of simplicity I will just use two lists.
So I have the following 2D lists:
> > a = [[1,2,3],
> > [1,2,3]]
> > b = [[4,5,6],
> > [4,5,6]]
And if I append list a and b, I'm looking to obtain the following:
masterlist = [[1,4], [2,5], [3,6], [1,4], [2,5], [3,6]]
The following code is what I have tried:
filenames = [a,b] #For the example, since I will have multiple arrays
masterlist = []
counter = 0
for file in filenames:
if counter == 0: #This if is to try to create lists within the list
for row in file: #This loops is to iterate throughout the whole list
for col in row:
c = [masterlist.append([col])]
[masterlist.append(c) for row, col in zip(masterlist, c)]
counter = 1
else: #This else is to append each element to their respective position
for row in file:
for col in row:
c = [masterlist.append(col)]
[masterlist.append([c]) for row, col in zip(masterlist, c)]
The output when printing masterlist is the following:
[[1], [2], [3], [1], [2], [3], [None], 4, 5, 6, 4, 5, 6, [[None]]]
I'm not sure where the [None]'s come from either. And as we can see '4,5,6...' aren't appended to the lists '[1], [2], [3]...' respectively.
You can iterate through the items of the lists and then add them to your masterlist:
a = [[1,2,3],
[1,2,3]]
b = [[4,5,6],
[4,5,6]]
masterlist = []
for aa,bb in zip(a,b): # loop over lists
for itema, itemb in zip(aa,bb): # loop over items in list
masterlist = masterlist + [[itema, itemb]]
output:
[[1, 4], [2, 5], [3, 6], [1, 4], [2, 5], [3, 6]]
If you use numpy,this is really easy
import numpy as np
a = np.array([[1,2,3],
[1,2,3]])
b = np.array([[4,5,6],
[4,5,6]])
fl = np.vstack(np.dstack((a,b)))
output
array([[1, 4],
[2, 5],
[3, 6],
[1, 4],
[2, 5],
[3, 6]])

Select elements from numpy array based on a list of lengths

Suppose I have a numpy array like this:
arr = np.array([[1,1,1,1], [2,2,2,2], [3,3,3,3], [4,4,4,4])
I also have a list that determines the intended lengths:
lens = [1,2,3,4]
Is there an elegant and Pythonic way to return a new array, with each corresponding element selected using the lens variable?
The output should be:
[[1], [2,2],[3,3,3], [4,4,4,4]]
If each Numpy arr list element size is less than 5 this works.
Use zip:
[a[:i].tolist() for a, i in zip(arr, lens)]
Output:
[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]]
This could be a possible solution:
res = []
for a,i in zip(arr, lens):
res.append(a[:i].tolist())
print(res)
Assuming arr is equal in length as lens:
[arr[i][:v].tolist() for i,v in enumerate(lens)]
Output: [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]]

sum columns of part of 2D array Python

INPUT:
M = [[1,2,3],
[1,2,3],
[1,2,3]]
how take the sum of the columns in the two first rows and implement it in the array M
OUTPUT:
M = [[2,4,6],
[1,2,3]]
Use numpy.add.reduceat:
import numpy as np
M = [[1,2,3],
[1,2,3],
[1,2,3]]
np.add.reduceat(M, [0, 2])
# indices [0,2] splits the list into [0,1] and [2] to add them separately,
# you can see help(np.add.reduceat) for more
# array([[2, 4, 6],
# [1, 2, 3]])
M = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
M[:2] = [[a + b for a, b in zip(M[0], M[1])]]
print(M) # [[5, 7, 9], [7, 8, 9]]
Things to google to understand this:
M[:2] =: python slice assignment
[... for .. in ...]: python list comprehension
for a, b in ...: python tuple unpacking loop

multiply all the integers in a list of lists python?

I want to multiply all the values in a list of lists in python:
input = 3*[[1,2,3],[3,2,1]]
so i get this output:
output = [[3,6,9],[9,6,3]]
i tried this:
l = [[1,2,3],[3,2,1]]
l = [i * 2 for i in x in l]
[[ 3 * i for i in inner ] for inner in outer]
>>> my_list = [[1,2,3],[3,2,1]]
>>> [map((3).__mul__, sublist) for sublist in my_list]
[[3, 6, 9], [9, 6, 3]]
You might consider using numpy arrays instead:
>>> import numpy as np
>>> a = np.array(my_list)
>>> a
array([[1, 2, 3],
[3, 2, 1]])
>>> 3*a
array([[3, 6, 9],
[9, 6, 3]])

Grouping Elements of Lists Within a List by Index

I am trying to take a list of lists, and return a list of lists which contain each element at an index of the original list of lists. I know that that's badly worded. Here's an example.
Say I have the following list of lists:
[[1,2,3], [4,5,6], [7,8,9]]
I want to get another list of lists, in which each list is a list of each elements at a specific index. For example:
[[1,2,3], [4,5,6], [7,8,9]] becomes [[1,4,7], [2,5,8], [3,6,9]]
So the first list in the returned list, contains all of the elements at the first index of each of the original list, and so on. I'm stuck, and have no idea how this could be done. Any help would be appreciated. Thanks.
>>> [list(t) for t in zip(*[[1,2,3], [4,5,6], [7,8,9]])]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
perhaps an easy way wiould be:
a=[[1,2,3], [4,5,6], [7,8,9]]
b=zip(*a)
b will be equal to [(1, 4, 7), (2, 5, 8), (3, 6, 9)].
hopes this helps
Dan D's answer is correct and will work in Python 2.x and Python 3.x.
If you're doing lots of matrix operations, not just transposition, it's worth considering Numpy at this juncture:
>>> import numpy as np
>>> x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> np.swapaxes(x, 0, 1)
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
Or, more simply, as per the comments, use numpy.transpose():
>>> np.transpose(x)
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
In addition to the zip(*lists) approach, you could also use a list comprehension:
[[l[i] for l in lists] for i in range(3)]
In case you want to simply transpose your matrix -e.g. to get new matrix where rows are cols from initial matrix while columns equal to the initial matrix rows values then you can use:
initMatr = [
[1,2,3],
[4,5,6],
[7,8,9]
]
map(list, zip(*initMatr))
>>> [
[1,4,7],
[2,5,8],
[3,6,9]
]
OR in case you want to rotate matrix left then:
map(list, zip(*map(lambda x: x[::-1], initMatr)
>>> [
[3,6,9],
[2,5,8],
[1,4,7]
]

Categories

Resources