【问题标题】:Longest word in a string using python programming使用python编程的字符串中最长的单词
【发布时间】:2017-06-20 10:27:00
【问题描述】:

大家好,我仍然是 python 的核心,希望有人可以帮助解决这个问题。

编写一个名为longest 的函数,它将接收一串以空格分隔的单词并返回最长的单词。 例如:

最长(“这太棒了”)=>“太棒了” 最长(“F”)=>“F”

class Test(unittest.TestCase):
 def test_longest_word(self):
        sentence = "This is Fabulous"
        self.assertEqual('Fabulous', longest(sentence))

 def test_one_word(self):
        sentence = "This"
        self.assertEqual("This", longest(sentence))

这是我目前的解决方案;

def find_longest_word(word_list):  
    longest_word = ''  
    longest_size = 0   
for word in word_list:    
    if (len(word) > longest_size)
    longest_word = word
    longest_size = len(word)      
return longest_word

words = input('Please enter a few words')  
word_list = words.split()  
find_longest_word(word_list) 

不幸的是,当我尝试测试代码时出现此错误 “文件”,第 6 行 if (len(word) > long_size) ^ SyntaxError: 无效语法

任何帮助,我将不胜感激?

【问题讨论】:

  • if (len(word) > longest_size)行的末尾添加:并缩进后三行

标签: python-3.x


【解决方案1】:
def find_longest_word(myText):
  a = myText.split(' ')
  return max(a, key=len)


text = "This is Fabulous"
print (find_longest_word(text)) #Fabulous

编辑:如果您想要最长的单词之一而不是全部单词,则上述解决方案有效。例如,如果我的文字是“嘿!你好吗?”它只会返回“嘿”。如果你想让它返回 ["Hey", "How", "are", "you"] 最好用这个。

def find_longest_word(myText):
  a = myText.split(' ')
  m = max(map(len,a))
  return [x for x in a if len(x) == m]

print (find_longest_word("Hey ! How are you ?"))  #['Hey', 'How', 'are', 'you']

See also, this question

【讨论】:

  • 收到此错误 Traceback: in NameError: name 'Fabulous' is not defined
  • 删除“// Fabulous”和“// ['Hey', 'How', 'are', 'you']” 我只是告诉你它应该打印什么跨度>
  • 需要小心 .split(" ") 因为这不会拆分所有空格。例如,它不会拆分“James/nBlunt”。如果要拆分多行文本,最好将 .split() 留空。
【解决方案2】:

if 语句末尾缺少 :

使用下面的更新代码,我也修复了您的缩进问题。

def find_longest_word(word_list):  
    longest_word = ''  
    longest_size = 0   
    for word in word_list:    
        if (len(word) > longest_size):
            longest_word = word
            longest_size = len(word)      
    return longest_word

 words = input('Please enter a few words')  
 word_list = words.split()  
 find_longest_word(word_list) 

【讨论】:

  • 谢谢,但现在收到“SyntaxError: 'return' outside function”这仍然是缩进错误吗?
  • 是的,还有一个额外的空间。我修好了它。你能再试一次吗?
【解决方案3】:

代码示例不正确。如果我尝试输出,我会收到以下消息:

第 15 行出错:print(longest_word("chair", "couch", "table"))

TypeError:longest_word() 接受 1 个位置参数,但给出了 3 个

所以代码看起来像这样:

def longest_word(word_list):  
    longest_word = ''  
    longest_size = 0   
    for word in word_list:    
        if (len(word) > longest_size):
            longest_word = word
            longest_size = len(word)      
    return longest_word
    words = input("chair", "couch", "table")  
    word_list = words.split()  
    find_longest_word(word_list) 

【讨论】:

    【解决方案4】:
    # longest word in a text
    text = input("Enter your text")
    #Create a list of strings by splitting the original string
    split_txt = text.split(" ")
    # create a dictionary as word:len(word)
    text_dic = {i:len(i)for i in split_txt}
    long_word = max([v for v in text_dic.values()])
    for k,v in text_dic.items():
        if long_word == v:
            print(k)
    

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 1970-01-01
      • 2018-11-25
      • 2018-04-18
      • 2020-03-04
      • 2016-06-14
      • 2017-07-04
      • 1970-01-01
      • 2020-08-22
      相关资源
      最近更新 更多