簡體   English   中英

確定字符串中是否有多個單詞

[英]Determine whether there’s more than one word in a string

例如:

line = 'how are you?'

if line == [has more than one word]: line.split()

這可能嗎?

嘗試這個:

line = 'how are you?'

if len(line.split()) > 1: # has more than 1 word

獲得line.split()似乎更pythonic,但如果這將是一個昂貴的操作,這可能會更快:

if ' ' in 'how are you?': line.split()
line.strip().count(' ') > 0 # finds at least one separator after removing the spaces at the left and the right of the actual string

或者如果你真的想要速度

line.strip().find(' ') != -1 # not finding the ' ' character in the string

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM