Trying to convert a string to list and then identify if there are int elements in the list Python

63 Views Asked by At

I have a string that I have converted to a list but I am unable to coerce some of the list values to int.

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"

for i in range(len(msg)):
    lst.append(msg[i]) #converted string msg to list called lst
print(lst)```
6

There are 6 best solutions below

1
erwanlfrt On

If you want to store all the int elements of your string in your lst array, with your code the simplest way to do that :

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
lst = []
for i in range(len(msg)):
  if (msg[i].isdigit()):
      lst.append(msg[i]) #converted string msg to list called lst
print(lst)
0
Paulius On

Are you trying to collect numeric chars?

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
lst = [el for el in msg if el.isnumeric()]
0
Chris Doyle On

you could check each char of the string and test if the value is a digit, using the any function you can stop checking once you find the first digit in the string if all you need to know if the string has digits

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
msg1 = "this has no ints"
print(f"contains ints: {any((char.isdigit() for char in msg))}")
print(f"contains ints: {any((char.isdigit() for char in msg1))}")

OUTPUT

contains ints: True
contains ints: False
0
ophact On
lst = [(int if c.isnumeric() else str)(c) for c in msg]

Of course, it doesn't follow best practices but it's nice and short. Converts the character to an integer if it's numeric.

0
alphaBetaGamma On

After making sure it's a digit, you append the value as int

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
lst = []
for i in range(len(msg)):
    if (msg[i].isdigit()):
        lst.append(int(msg[i]))
    else:
        lst.append(msg[i])
print(lst)

output:

['M', 'e', 'e', 't', ' ', 'm', 'e', ' ', 'a', 't', ' ', 't', 'h', 'e', ' ', 'R', 'i', 't', 'z', ' ', 'C', 'a', 'r', 'l', 't', 'o', 'n', ' ', 'a', 't', ' ', 9, ' ', 'o', "'", 'c', 'l', 'o', 'c', 'k', ',', ' ', 'd', 'o', 'n', "'", 't', ' ', 'b', 'e', ' ', 'l', 'a', 't', 'e', '!']
0
raiyan22 On

Case 1.1

If you want to extract the words from the string given, This one makes a list of words from the given string :

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
res = msg.split()
print ( str(res) )

Output:

['Meet', 'me', 'at', 'the', 'Ritz', 'Carlton', 'at', '9', "o'clock,", "don't", 'be', 'late!']

Case 1.2

If you want to extract the words from the string given, This one makes a list of words from the given string but it stores the numeric values as integer:

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
res = msg.split()
for index, item in enumerate(res):
    if item.isnumeric():
        res[index] = int(item)
        
print ( str(res) )

Output: (notice the number is stored as a int here, not as a string like the previous one)

['Meet', 'me', 'at', 'the', 'Ritz', 'Carlton', 'at', 9, "o'clock,", "don't", 'be', 'late!']

Case 2.1

If you want to list each of the characters from the string given, use this

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
print(list(msg))

Output:

['M', 'e', 'e', 't', ' ', 'm', 'e', ' ', 'a', 't', ' ', 't', 'h', 'e', ' ', 'R', 'i', 't', 'z', ' ', 'C', 'a', 'r', 'l', 't', 'o', 'n', ' ', 'a', 't', ' ', '9', ' ', 'o', "'", 'c', 'l', 'o', 'c', 'k', ',', ' ', 'd', 'o', 'n', "'", 't', ' ', 'b', 'e', ' ', 'l', 'a', 't', 'e', '!']

Case 2.2

If you want to list each of the characters from the string given but want to store the numeric value as an integer within the list then:

msg = "Meet me at the Ritz Carlton at 9 o'clock, don't be late!"
res = [ (int if x.isnumeric() else str) (x) for x in msg ] 
print(res)

Output (notice the number is stored as a int here, not as a string like the previous one):

['M', 'e', 'e', 't', ' ', 'm', 'e', ' ', 'a', 't', ' ', 't', 'h', 'e', ' ', 'R', 'i', 't', 'z', ' ', 'C', 'a', 'r', 'l', 't', 'o', 'n', ' ', 'a', 't', ' ', 9, ' ', 'o', "'", 'c', 'l', 'o', 'c', 'k', ',', ' ', 'd', 'o', 'n', "'", 't', ' ', 'b', 'e', ' ', 'l', 'a', 't', 'e', '!']