-
-
Save bbookman/ad4a1d6e9e9c752bf5ec899de0ca82dc to your computer and use it in GitHub Desktop.
''' | |
Get only the numbers in a sentence like 'In 1984 there were 13 instances of a protest with over 1000 people attending'. Result is a list of numbers like [3,4,5] | |
''' | |
sentence = 'In 1984 there were 13 instances of a protest with over 1000 people attending' | |
words = sentence.split() | |
result = [number for number in words if not number.isalpha() ] | |
print(result) |
sentence = 'In 1984 there were 13 instances of a protest with over 1000 people attending'
numbers = [ num for num in sentence if num in [ str(x) for x in range(0,10)]
print(numbers)
import re
[i for i in "In 1984 there were 13 instances of a protest with over 1000 people attending".split() if re.match("[0-9]", i)]
str2 = "In 1984 there were 13 instances of a protest with over 1000 people attending"
only_nums = [ x for x in str2.split() if x.isdigit() ]
print( only_nums )
sen = "In 1984 there were 13 instances of a protest with over 1000 people attending"
nums = [i for i in sen.split() if i.isdigit()]
print(nums)
import re
sentence = 'In 1984 there were 13 instances of a protest with over 1000 people attending'
w_in_sentence = sentence.split()
pattern = r'[0-9]'
print([n for n in sentence if re.match(pattern, n)])
[j for j in sentence.split() if j.isdigit()]
string = 'In 1984 there were 13 instances of a protest with over 1000 people attending'
li = [i for i in string if i.isdigit()]
print(li)
s = filter(lambda x : x.isdigit(),string)
print(list(s))
import re
string = 'In 1984 there were 13 instances of a protest with over 1000 people attending'
print(re.findall(r'[0-9]',string))
sentence = 'In 1984 there were 13 instances of a protest with over 1000 people attending' [int(letter) for letter in sentence if letter in ['1','2','3','4','5','6','7','8','9','0']]
Unfortunately, it does not work, because the question is not about digits
sentence = 'In 1984 there were 13 instances of a protest with over 1000 people attending'
numbers = [ num for num in sentence if num in [ str(x) for x in range(0,10)]
print(numbers)
your code will print digits, it's not question goal
string = "In 1984 there were 13 instances of a protest with over 13 people attending".split()
print(string)
list_a = [a for a in string if a[0] in ("1","2","3","4","5","6","7","8","9","0") ]
print(list_a)
# Get only the numbers in a sentence
sentence = 'In 1984 there were 13 instances of a protest with over 1000 people attending'
my_list = [item for item in sentence if item.isnumeric()]
print(my_list)
sentence = 'In 1984 there were 13 instances of a protest with over 1000 people attending'
#If we were about to return one integer at a time like [1, 9, 8, 4, 1, 3, 1, 0, 0, 0] rather than 1984, 13, 1000 we would do it like this:
nums = [number for number in sentence if number.isdigit() == True]
print(nums)
STR = 'In 1984 there were 13 instances of a protest with over 1000 people attending'
numbers_in_str_automatic = [numerical_values for numerical_values in STR.split(' ') if int(numerical_values.isdigit())]
print(numbers_in_str_automatic)
"""
Get only the numbers in a sentence like
'In 1984 there were 13 instances of a protest with over 1000 people attending'
"""
string = 'In 1984 there were 13 instances of a protest with over 1000 people attending'
only_num = [int(i) for i in string if i in '1234567890']
str1="In 1984 there were 13 instances of a protest with over 1000 people attending"
res=[x for x in str1 if ord(x)>=48 and ord(x)<=57]
print(res)
state = 'In 1984 there were 13 instances of a protest with over 1000 people attending'
st_list = [word for word in state.split() if word.isdigit()]
print(st_list)