考虑到标点符号,如何反转字符串中的单词?
How to reverse the words of a string considering the punctuation?
这是我目前的情况:
def reversestring(thestring):
words = thestring.split(' ')
rev = ' '.join(reversed(words))
return rev
stringing = input('enter string: ')
print(reversestring(stringing))
我知道我遗漏了一些东西,因为我需要标点符号来遵循逻辑。
所以假设用户输入 Do or do not, there is no try.
。结果应该是 .try no is there , not do or Do
,但我只得到 try. no is there not, do or Do
。我使用一个简单的实现来反转字符串中的所有字符,然后做一些检查所有单词并再次反转字符的操作,但仅限于具有字母 ASCII 值的字符。
您的代码完全按照其应有的方式执行,在 space 上拆分不会将点 ro 逗号与单词分隔开。
我建议您使用 re.findall
来获取您感兴趣的所有单词和所有标点符号
import re
def reversestring(thestring):
words = re.findall(r"\w+|[.,]", thestring)
rev = ' '.join(reversed(words))
return rev
reversestring("Do or do not, there is no try.") # ". try no is there , not do or Do"
试试这个(代码注释中的解释):
s = "Do or do not, there is no try."
o = []
for w in s.split(" "):
puncts = [".", ",", "!"] # change according to needs
for c in puncts:
# if a punctuation mark is in the word, take the punctuation and add it to the rest of the word, in the beginning
if c in w:
w = c + w[:-1] # w[:-1] gets everthing before the last char
o.append(w)
o = reversed(o) # reversing list to reverse sentence
print(" ".join(o)) # printing it as sentence
#output: .try no is there ,not do or Do
您可以使用正则表达式将句子解析为单词列表和分隔符列表,然后将单词列表反转并组合在一起形成所需的字符串。您的问题的解决方案如下所示:
import re
def reverse_it(s):
t = "" # result, empty string
words = re.findall(r'(\w+)', s) # just the words
not_s = re.findall(r'(\W+)', s) # everything else
j = len(words)
k = len(not_s)
words.reverse() # reverse the order of word list
if re.match(r'(\w+)', s): # begins with a word
for i in range(k):
t += words[i] + not_s[i]
if j > k: # and ends with a word
t += words[k]
else: # begins with punctuation
for i in range(j):
t += not_s[i] + words[i]
if k > j: # ends with punctuation
t += not_s[j]
return t #result
def check_reverse(p):
q = reverse_it(p)
print("\"%s\", \"%s\"" % (p, q) )
check_reverse('Do or do not, there is no try.')
输出
"Do or do not, there is no try.", "try no is there, not do or Do."
这不是一个非常优雅的解决方案,但确实有效!
这是我目前的情况:
def reversestring(thestring):
words = thestring.split(' ')
rev = ' '.join(reversed(words))
return rev
stringing = input('enter string: ')
print(reversestring(stringing))
我知道我遗漏了一些东西,因为我需要标点符号来遵循逻辑。
所以假设用户输入 Do or do not, there is no try.
。结果应该是 .try no is there , not do or Do
,但我只得到 try. no is there not, do or Do
。我使用一个简单的实现来反转字符串中的所有字符,然后做一些检查所有单词并再次反转字符的操作,但仅限于具有字母 ASCII 值的字符。
您的代码完全按照其应有的方式执行,在 space 上拆分不会将点 ro 逗号与单词分隔开。
我建议您使用 re.findall
来获取您感兴趣的所有单词和所有标点符号
import re
def reversestring(thestring):
words = re.findall(r"\w+|[.,]", thestring)
rev = ' '.join(reversed(words))
return rev
reversestring("Do or do not, there is no try.") # ". try no is there , not do or Do"
试试这个(代码注释中的解释):
s = "Do or do not, there is no try."
o = []
for w in s.split(" "):
puncts = [".", ",", "!"] # change according to needs
for c in puncts:
# if a punctuation mark is in the word, take the punctuation and add it to the rest of the word, in the beginning
if c in w:
w = c + w[:-1] # w[:-1] gets everthing before the last char
o.append(w)
o = reversed(o) # reversing list to reverse sentence
print(" ".join(o)) # printing it as sentence
#output: .try no is there ,not do or Do
您可以使用正则表达式将句子解析为单词列表和分隔符列表,然后将单词列表反转并组合在一起形成所需的字符串。您的问题的解决方案如下所示:
import re
def reverse_it(s):
t = "" # result, empty string
words = re.findall(r'(\w+)', s) # just the words
not_s = re.findall(r'(\W+)', s) # everything else
j = len(words)
k = len(not_s)
words.reverse() # reverse the order of word list
if re.match(r'(\w+)', s): # begins with a word
for i in range(k):
t += words[i] + not_s[i]
if j > k: # and ends with a word
t += words[k]
else: # begins with punctuation
for i in range(j):
t += not_s[i] + words[i]
if k > j: # ends with punctuation
t += not_s[j]
return t #result
def check_reverse(p):
q = reverse_it(p)
print("\"%s\", \"%s\"" % (p, q) )
check_reverse('Do or do not, there is no try.')
输出
"Do or do not, there is no try.", "try no is there, not do or Do."
这不是一个非常优雅的解决方案,但确实有效!