У меня есть назначение в моем классе Data Structures, и я использую Python, чтобы попытаться его решить. Я действительно застрял и ржавый в Python, поэтому, пожалуйста, несите меня.Разделите предложение на слова, используя символ пробела в качестве разделителя в Python
Проблема
Read a sentence from the console.
Break the sentence into words using the space character as a delimiter.
Iterate over each word, if the word is a numeric
value then print its value doubled, otherwise print out the word,
with each output on its own line.
Sample Run:
Sentence: Hello world, there are 3.5 items.
Output:
Hello
world,
there
are
7
items.
мой код до сих пор ...
import string
import re
def main():
string=input("Input a sentence: ")
wordList = re.sub("[^\w]", " ", string).split()
print("\n".join(wordList))
main()
Это дает мне этот выход:
>>>
Input a sentence: I like to eat 7 potatoes at a time
I
like
to
eat
7
potatoes
at
a
time
>>>
Так что моя задача выяснить, как извлекая числовой и затем удваивает его. Я не знаю, с чего начать.
Любая обратная связь всегда приветствуется. Спасибо!
Вы можете проверить, является ли строка числом в Python: http://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-in-python –