Python Forum
converting list of zero length to a matrix of 3*3
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
converting list of zero length to a matrix of 3*3
#1
I have a list (length of a list is one) [[1,2,3],[4,5,6]], I like to convert it to a matrix (2*3). something like this [1 2 3 
      4 5 6]

This is an example of a real problem, therefore not putting my large code here. Thanks
Reply
#2
The example is a list of 2, with each element having 3. That said, you want to flatten the list to be 1 by 6.
mylist = [[1,2,3],[4,5,6]]
flatlist = [item for sublist in mylist for item in sublist]
print(flatlist)
Output:
[1, 2, 3, 4, 5, 6]
Reply
#3
There is no such thing as a Python matrix. You can have a list of lists, like you show in your example. If you are using numpy you can have an array of arrays.
m = np.array([[1, 2, 3], [4, 5, 6]))
m can have matrix like things done to it like m.transpose() or m.dot(anotherM)

There used to be a matrix class in numpy, but I think it is depreciated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 790 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  The list length should be 100 but it is more than 100 quest 1 1,344 Jul-30-2021, 09:24 AM
Last Post: quest
  Converting a list to dictinary tester_V 8 2,637 Jul-02-2021, 09:04 PM
Last Post: tester_V
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,304 May-03-2021, 06:30 AM
Last Post: Gribouillis
  Converting tkinter listbox into list BigSwiggy 6 3,527 Feb-07-2021, 02:01 PM
Last Post: BigSwiggy
  Converting list to variables Palves 1 1,735 Sep-18-2020, 05:43 PM
Last Post: stullis
  Trouble with converting list , dict to int values! faryad13 7 3,671 Sep-04-2020, 06:25 AM
Last Post: faryad13
  converting string object inside a list into an intiger bwdu 4 2,555 Mar-31-2020, 10:36 AM
Last Post: buran
  more list help converting paul41 3 2,396 Nov-25-2019, 07:59 AM
Last Post: perfringo
  List Comprehension - Creating a list of the length of an item help paul41 2 2,017 Nov-18-2019, 10:21 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020