Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python receiving error using ord function "TypeError: ord() expected a character, but string of length 2 found"

I have modified some Python code found, which calculates the transition matrix for an observed sequence (transitions named here).

For some reason it works fine, but when I put a number above or equal to "10" I get this error:

"ord() expected a character, but string of length 2 found"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

states_no = 10
transitions = [['10', '1', '1', '2', '3', '1']]
trans_matr = []

for k in range(len(transitions)):


    def rank(c):
        return ord(c) - ord('1')

    T = [rank(c) for c in transitions[k]]
    #print(transitions[k])
    #create matrix of zeros

    M = [[0]*states_no for _ in range(states_no)]

    for (i,j) in zip(T,T[1:]):
        M[i][j] += 1

    #now convert to probabilities:
    for row in M:
        n = sum(row)
        if n > 0:
            row[:] = [f/sum(row) for f in row]

    #print M:
    trans_matr.append(M)
    #for row in M:
        #print(row)
#print(trans_matr)

trans_matr_array=numpy.array(trans_matr)
print(trans_matr_array)

I guess it has something to do with the character recognition. Is there any way to put numbers up to 14 without getting this error?

>Solution :

As far as I can tell the code uses ord() only for calculating the difference between the input and ‘1’. Does your input include non-numeric characters?
If not, simply change the function "rank":

def rank(c)
    return int(c) - 1

I assume you only need integers, you can change it to float to accept any number.

If you need to also accept non-numeric characters:

def rank(c):
    if c.isnumeric():
        return int(c) - 1
    elif len(c) == 1:
        return ord(c) - ord('1')
    else:
        raise TypeError('non numeric inputs should have length 1')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading