+ 2
Code Not Correct
https://code.sololearn.com/c0ril2MdBaCc/?ref=app I want to select out all the digits from a sentence and coded the program but don't know what is going wrong with this. Can someone hep me out with this. P.S. : I've edited the program and now it's running just fine
3 odpowiedzi
+ 1
But it works fine.
But you can just convert the char to int before appending to avoid all the quotes.
#python 3.7.1
def NumExt(words):
numlist = list()
for i in words:
for char in i:
if char.isdigit():
numlist.append(int(char))
return numlist
sentence = input()
words = sentence.split(" ")
print(NumExt(words))
+ 10
# try this-
def NumExt(words):
numlist = []
for i in words:
try:
val = int(i)
numlist.append(val)
except ValueError:
continue
return numlist
str = "1solve5d7"
print(NumExt(str))
# output: [1,5,7]
+ 5
A little more compact, using regex:
import re
ptrn = re.compile(r'(\d)')
s = 'sghs23sa8e43aajhsj8'
print(list(map(int, ptrn.findall(s))))