Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 175751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:45:17+00:00 2026-05-11T13:45:17+00:00

Yesterday I had the need for a matrix type in Python. Apparently, a trivial

  • 0

Yesterday I had the need for a matrix type in Python.

Apparently, a trivial answer to this need would be to use numpy.matrix(), but the additional issue I have is that I would like a matrix to store arbitrary values with mixed types, similarly to a list. numpy.matrix does not perform this. An example is

>>> numpy.matrix([[1,2,3],[4,'5',6]]) matrix([['1', '2', '3'],         ['4', '5', '6']],         dtype='|S4') >>> numpy.matrix([[1,2,3],[4,5,6]]) matrix([[1, 2, 3],         [4, 5, 6]]) 

As you can see, the numpy.matrix must be homogeneous in content. If a string value is present in my initialization, every value gets implicitly stored as a string. This is also confirmed by accessing the single values

>>> numpy.matrix([[1,2,3],[4,'5',6]])[1,1] '5' >>> numpy.matrix([[1,2,3],[4,'5',6]])[1,2] '6' 

Now, the Python list type can instead accept mixed types. You can have a list containing an integer and a string, both conserving their type. What I would need is something similar to a list, but operating in a matrix-like behavior.

Therefore, I had to implement my own type. I had two choices for the internal implementation: list containing lists, and dictionaries. Both solutions have shortcomings:

  • list of lists require careful synchronization of the various lists’ sizes. Swapping two rows is easy. Swapping two columns is less easy. Removing a row is easy as well.
  • dictionaries (with a tuple as a key) are slightly better, but you have to define the limits of your key (for example, you cannot insert element 5,5 if your matrix is 3×3), and they are more complex to use to insert, remove, or swap columns or rows.

Edit: clarification. The concrete reason on why I need this functionality is because I am reading CSV files. Once I collect the values from a CSV file (values that can be string, integers, floats) I would like to perform swapping, removal, insertion and other operations alike. For this reason I need a ‘matrix list’.

My curiosities are:

  • do you know if a Python data type providing this service already exists (maybe in a ‘non-battery included’ library out there)?
  • why is this data type not provided in the standard library? Too restricted interest maybe?
  • How would you have solved this need? Dictionary, list, or another smarter solution?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:45:17+00:00Added an answer on May 11, 2026 at 1:45 pm

    You can have inhomogeneous types if your dtype is object:

    In [1]: m = numpy.matrix([[1, 2, 3], [4, '5', 6]], dtype=numpy.object) In [2]: m Out[2]:  matrix([[1, 2, 3],         [4, 5, 6]], dtype=object) In [3]: m[1, 1] Out[3]: '5' In [4]: m[1, 2] Out[4]: 6 

    I have no idea what good this does you other than fancy indexing, because, as Don pointed out, you can’t do math with this matrix.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Phalanger is what you're looking for. May 11, 2026 at 5:01 pm
  • Editorial Team
    Editorial Team added an answer I had to modify Jacob Carpenter's answer but it works… May 11, 2026 at 5:01 pm
  • Editorial Team
    Editorial Team added an answer Set the regular expression engine to Perl (on the advanced… May 11, 2026 at 5:01 pm

Related Questions

I'm writing this question with reference to this one which I wrote yesterday. After
This is a follow up on another problem i had with getting-the-last-record-inserted-into-a-select-query I am
I've seen quite a few posts on changes in .NET 3.5 SP1, but stumbled
I read all over the Internet (various sites and blogs) about version control. How

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.