hey gues
i search 'replace' matrix?
123
456
789
i need in output
147
258
369
Jump to PostYou need to give us more info!
Jump to PostAn excellent use of module numpy ...
# transpose a Python list of lists (matrix) # using module numpy from http://sourceforge.net/projects/numpy # tested with Python 2.6.5 import numpy as np …
Jump to PostThere is the classical
zip(*list)
:>>> matrix = [[1,2,3],[4,5,6],[7,8,9]] >>> zip(*matrix) [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
You need to give us more info!
my code
def y():
matrix = [ "12.34, 67.89, 56.21",
"22.01, 11.0, 34.56",
"20.0, 56, 0" ]
a = [x.split(", ") for x in matrix]
In polsih this word name is 'tranpozycja' ( in math ).
An excellent use of module numpy ...
# transpose a Python list of lists (matrix)
# using module numpy from http://sourceforge.net/projects/numpy
# tested with Python 2.6.5
import numpy as np
# a Python list of lists (made to look pretty)
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# change to a numpy matrix
matrix_np = np.array(matrix)
print( matrix_np )
"""my result -->
[[1 2 3]
[4 5 6]
[7 8 9]]
"""
matrix_np_transposed = np.transpose(matrix_np)
print( matrix_np_transposed )
"""my result -->
[[1 4 7]
[2 5 8]
[3 6 9]]
"""
# change back into a Python list of lists (matrix)
matrix_transposed = matrix_np_transposed.tolist()
print( matrix_transposed )
"""my result (made pretty) -->
[
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]
]
"""
i don't want libary, i want new function! ;p
There is the classical zip(*list)
:
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> zip(*matrix)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
Also
>>> n=10
>>> a=[list(range(n)) for y in range(n)]
>>> from pprint import pprint as pp
>>> pp(a)
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
>>> pp(map(None,*a))
[(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
(2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
(3, 3, 3, 3, 3, 3, 3, 3, 3, 3),
(4, 4, 4, 4, 4, 4, 4, 4, 4, 4),
(5, 5, 5, 5, 5, 5, 5, 5, 5, 5),
(6, 6, 6, 6, 6, 6, 6, 6, 6, 6),
(7, 7, 7, 7, 7, 7, 7, 7, 7, 7),
(8, 8, 8, 8, 8, 8, 8, 8, 8, 8),
(9, 9, 9, 9, 9, 9, 9, 9, 9, 9)]
>>>
(zip stops in shortest iterable)
And if you happen to need to make the shorter lines longer by filling by None. You can do howerver map(None,*matrix)
>>> a=[list(range(y)) for y in range(n)]
>>> pp(a)
[[],
[0],
[0, 1],
[0, 1, 2],
[0, 1, 2, 3],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5, 6],
[0, 1, 2, 3, 4, 5, 6, 7],
[0, 1, 2, 3, 4, 5, 6, 7, 8]]
>>> pp(zip(*a))
[]
>>> pp(map(None,*a))
[(None, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(None, None, 1, 1, 1, 1, 1, 1, 1, 1),
(None, None, None, 2, 2, 2, 2, 2, 2, 2),
(None, None, None, None, 3, 3, 3, 3, 3, 3),
(None, None, None, None, None, 4, 4, 4, 4, 4),
(None, None, None, None, None, None, 5, 5, 5, 5),
(None, None, None, None, None, None, None, 6, 6, 6),
(None, None, None, None, None, None, None, None, 7, 7),
(None, None, None, None, None, None, None, None, None, 8)]
>>>
One problem is that
map(None, *matrix)
will not work with Python3
However
list(zip(*matrix))
does work with Python3
And with few iterations of interactive testing the triangle matrix can be got back by :icon_wink:
pp(map(lambda x: filter(None,x),zip(*map(None,*a))))
Surely not most straigt forward thing but I got just curious.
If you got confused:
Do transform Map(None way
Undo it zip(* way
Filter Nones by map filter with lambda function for standard parameter None
vegaseat:
In python2.6 zip(*matrix) seem to work without list i.e list(zip(*matrix))==zip(*matrix)
Tony
I want new function, i dont want use this function ...
New function == algorythims tranpose matrix
Look, if you want a new function, and you're sure that it'll always be a 3x3 matrix, think about what you need to do. Outline it. Do it by hand a few times, and then list EVERY step needed to do it. Then translate it into the python code. So let me show you something:
# Our 3x3 matrix
matrix = \
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
# What we want it as:
new_matrix = \
[
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]
]
# What steps do we need?
# If we take a look at the number placements, we get this:
# matrix[0][0] = 1
# matrix[0][1] = 2
# matrix[0][2] = 3
# matrix[1][0] = 4
# matrix[1][1] = 5
# matrix[1][2] = 6
# matrix[2][0] = 7
# matrix[2][1] = 8
# matrix[2][2] = 9
# We want it to look like this:
# new_matrix[0][0] = 1
# new_matrix[0][1] = 4
# new_matrix[0][2] = 7
# new_matrix[1][0] = 2
# new_matrix[1][1] = 5
# new_matrix[1][2] = 8
# new_matrix[2][0] = 3
# new_matrix[2][1] = 6
# new_matrix[2][2] = 9
# What steps can we take to make this possible?
If you need further help, don't be hesitant to ask
Why use new function when it is already built in the language zip(*matrix)
or map(None,*matrix)
You want to do format checking or something?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.