[Python] How to convert matrix of binary numbers into matrix of strings/symbols? (Python/Sage)

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have a matrix in python consisting of 1s and 0s. As an example:

    m= [1 0 1]
    [1 1 1]
    [0 0 1]


    I would like to convert it to a matrix of symbols/strings. An example of the output I'm looking for :

    m_str= [A 0 C]
    [A B C]
    [0 0 C]


    I thought I might be able to just... multiply the matrix by a vector of strings [A, B, C] and have it pop out the above matrix, calculating Ax0 to 0 and Ax1 to A. However looking into it, this is not a matter of simply saying "multiply them and print".

    Currently my code is just yelling at me that you can't do:

    Mlet=vector (["A","B","C"])


    Which in hindsight is obvious.

    Is there a way to make this work that is better than iterating through every element of the matrix going "if 1, put the letter in its place"? Technically I don't even need it to remain as a matrix. A string for each row/column is sufficient.

    I'm using python with Sage Math through CoCal (personal server), but truly have little experience with python and even less with Sage. (It did what I needed to GET my matrix, but thats all I know.)

    Continue reading...

Compartilhe esta Página