更快更简洁的方法来反转字符串中的句子?

时间:2014-03-01 02:51:42

标签: python string algorithm

好的,我正在寻找一种快速简洁的方式来反转字符串中句子的方法。

例如,如果我想转换“”一二三。四五六。“至”三二一。六五四。“

这是我的天真方法:

def reverse_sentences(str):
 newlist=str.split(".")   
 newstr=''
 for s in newlist[0:-1]:
   words=s.split()
   words=words[::-1]
   words[0]=words[0][0].upper()+words[0][1:]
   words[-1]=words[-1][0].lower()+words[-1][1:]
   for e in range(len(words)):
      words[e].strip()
      if words[e] != words[-1]:
           newstr+=words[e]+" "
      else:
           newstr+=words[e]
   newstr+=". "
 return newstr

任何更快,更Pythonic的方式来做到这一点,例如列表理解和东西。

2 个答案:

答案 0 :(得分:4)

这是一个很长的单行使用列表理解切片str.join

In [79]: t="One two three. Four five six."

In [80]: ' '.join(' '.join(sentence.lower().split()[::-1]).capitalize()+'.'
    ...:                              for sentence in t.split('.')[:-1])
Out[80]: 'Three two one. Six five four.'

答案 1 :(得分:2)

我可能会使用generator expressions(对于lazy evaluation)来写这样的内容:

def reverse_sentences(text):
    sentences = text.split('.')
    reversed_words = (reversed(words.split()) for words in sentences)
    rejoined_words = (' '.join(words) for words in reversed_words)
    capitalized_sentences = (sentence.capitalize() for sentence in rejoined_words)
    return '. '.join(capitalized_sentences)

reverse_sentences("One two three. Four five six.")

我喜欢这个函数而不是“单行”,因为它更容易维护(例如,如果你想为标点符号添加处理,或strip - ping空格)。

将其分解为其组成部分(使用list comprehensions进行说明):

In [1]: text = "One two three. Four five six."

In [2]: text
Out[2]: 'One two three. Four five six.'

In [3]: sentences = text.split('.')

In [4]: sentences
Out[4]: ['One two three', ' Four five six', '']

In [5]: reversed_words = [words.split()[::-1] for words in sentences]

In [6]: reversed_words
Out[6]: [['three', 'two', 'One'], ['six', 'five', 'Four'], []]

In [7]: rejoined_words= [' '.join(words) for words in reversed_words]

In [8]: rejoined_words
Out[8]: ['three two One', 'six five Four', '']

In [9]: capitalized_sentences = [sentence.capitalize() for sentence in rejoined_words]

In [10]: capitalized_sentences
Out[10]: ['Three two one', 'Six five four', '']

In [11]: '. '.join(capitalized_sentences)
Out[11]: 'Three two one. Six five four. '

注意:

速度

您可能想知道 运行速度有多快...

timeit在这里很方便。

Your example

> python -m timeit <<EOF
def reverse_sentences(str):
 newlist=str.split(".")
 newstr=''
 for s in newlist[0:-1]:
   words=s.split()
   words=words[::-1]
   words[0]=words[0][0].upper()+words[0][1:]
   words[-1]=words[-1][0].lower()+words[-1][1:]
   for e in range(len(words)):
      words[e].strip()
      if words[e] != words[-1]:
           newstr+=words[e]+" "
      else:
           newstr+=words[e]
   newstr+=". "
 return newstr

reverse_sentences("One two three. Four five six.")
EOF
100000000 loops, best of 3: 0.012 usec per loop

My example

> python -m timeit <<EOF
def reverse_sentences(text):     
    sentences = text.split('.')                                                                      
    reversed_words = (reversed(words.split()) for words in sentences)
    rejoined_words = (' '.join(words) for words in reversed_words)
    capitalized_sentences = (sentence.capitalize() for sentence in rejoined_words)
    return '. '.join(capitalized_sentences)

reverse_sentences("One two three. Four five six.")
EOF
100000000 loops, best of 3: 0.012 usec per loop

zhangxaochen's example

> python -m timeit <<EOF
t="One two three. Four five six."
' '.join(' '.join(sentence.lower().split()[::-1]).capitalize()+'.' for sentence in t.split('.')[:-1])
EOF                                                                  
100000000 loops, best of 3: 0.012 usec per loop

结论

通过遵守The Zen of Python来优化可读性和可维护性,您的代码将更“pythonic”。