2

So I was testing different methods of omitting any characters in Python from string that weren't numbers.

When using this particular method below, I found that some characters from the Latin alphabet remained after this method:

Code

def get_lengths():
    books = []
    while True:
        book = input()
        for a in book:
            books.append(a)
        if book in ['stop', 'Stop']:
            break
    numbers = '0123456789'
    characters = list(books)
    print(characters)
    for b in characters:
        if b not in list(numbers):
            del characters[characters.index(b)]
    print(characters)

Input

144 Leaves of Grass
766 game of Thrones
stop

Output

['1', '4', '4', ' ', 'L', 'e', 'a', 'v', 
'e', 's', ' ', 'o', 'f', ' ', 'G', 'r', 
'a', 's', 's', '7', '6', '6', ' ', 'g', 
'a', 'm', 'e', ' ', 'o', 'f', ' ', 'T', 
'h', 'r', 'o', 'n', 'e', 's', 's', 't', 
'o', 'p']

['1', '4', '4', 'L', 'f', 'G', 'a', '7', 
'6', '6', 'g', 'a', 'm', ' ', 'f', ' ', 
'T', 'r', 'n', 'e', 's', 's', 't', 'p']

The first half of the output is the string before the omission. The second half deals with the string after the omission, where only the numbers should remain.

So my question is, why do various characters from the Latin alphabet remain when all non-number characters were programmed to be deleted from the string?

P.S. I'm aware that this is a somewhat impractical method to omit all characters that aren't numbers, however keep in mind that I was testing several different methods of doing so.

Also, don't worry about the variable names, I was working on a different project when I started to test these methods.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80

1 Answers1

-1

This is happening because of ASCII comparision. Computers doesn't understands anything except 0 & 1. Plzz convert numbers to list, see if that works. Otherwise ask again.