鱼C论坛

 找回密码
 立即注册
查看: 2021|回复: 13

[已解决]找出一个短句中字符最多的单词

[复制链接]
发表于 2021-7-18 16:00:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. sentence = 'Today is a perfect day!'   # 输入英文短句如 Today is a perfect day!

  2. words = sentence.split()   # 按照空格分裂

  3. list =[]

  4. maxlength = 0

  5. for word in words:
  6.     if word.isalpha():      #如果word 全为字母的话
  7.         if len(word) >= maxlength:
  8.             list = [word]
  9.             maxlength = len[word]
  10.             list.append(word)
  11.     else:
  12.         len(word[:-1]) == maxlength:  # 排查文末有标点的特殊情况,去掉最后一个字符
  13.             list.append(word)

  14. print(list)
复制代码



需求: 从一个英文短句中找出字符最多的那个单词 (可能不只一个)

上面的我的代码,但是显示else 下面的一句报错,请假下大神是什么原因啊,或者说我这个思路是否正确呢,感谢指点!
最佳答案
2021-7-18 17:17:28
weiyideid823 发表于 2021-7-18 16:59
Today is a perfect dayyyyy!

如果句子是这个,好像不能同时把perfect 和dayyyyy 打印出来啊
  1. sentence = 'Today is a perfect day!'   # 输入英文短句如 Today is a perfect day!

  2. words = sentence.split()   # 按照空格分裂

  3. maxlength = 0
  4. for word in words:    # 确定最长单词的长度值
  5.     if not word . isalpha():
  6.         word = word[:-1]
  7.     if len(word) > maxlength:
  8.         maxlength = len(word)

  9. for word in words:    # 把符合最大长度的所有单词打印出来
  10.     if not word . isalpha():
  11.         word = word[:-1]
  12.     if len(word) == maxlength:
  13.         print(word)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-18 16:18:15 | 显示全部楼层
       这是一个逻辑表达式
  1. len(word[:-1]) == maxlength:
复制代码

       应该用 if 打头
  1. if len(word[:-1]) == maxlength:
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-18 16:24:40 | 显示全部楼层
jackz007 发表于 2021-7-18 16:18
这是一个逻辑表达式

       应该用 if 打头

啊 这么低级的错误,感谢大大神
  1. sentence = 'today is a perfect day!'   # 输入英文短句如 Today is a perfect day!

  2. words = sentence.split()   # 按照空格分裂

  3. list =[]

  4. 另外我改动之后,最后运行出来的结果是把today 和 perfect 都打印出来了,看来我这if else 的逻辑有问题?

  5. maxlength = 0

  6. for word in words:
  7.     if word.isalpha():      #如果word 全为字母的话
  8.         if len(word) >= maxlength:
  9.             maxlength = len(word)
  10.             list.append(word)
  11.     else:
  12.         if len(word[:-1]) == maxlength:  # 排查文末有标点的特殊情况,去掉最后一个字符
  13.             list.append(word)

  14. print(list)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-18 16:32:33 | 显示全部楼层
jackz007 发表于 2021-7-18 16:18
这是一个逻辑表达式

       应该用 if 打头

我改了下,然后结果把taday 和 perfect 都打印出来了,怎样改正我这个if else 的逻辑啊

  1. sentence = 'today is a perfect day!'   # 输入英文短句如 Today is a perfect day!

  2. words = sentence.split()   # 按照空格分裂

  3. list =[]

  4. 另外我改动之后,最后运行出来的结果是把today 和 perfect 都打印出来了,看来我这if else 的逻辑有问题?

  5. maxlength = 0

  6. for word in words:
  7.     if word.isalpha():      #如果word 全为字母的话
  8.         if len(word) >= maxlength:
  9.             maxlength = len(word)
  10.             list.append(word)
  11.     else:
  12.         if len(word[:-1]) == maxlength:  # 排查文末有标点的特殊情况,去掉最后一个字符
  13.             list.append(word)

  14. print(list)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-18 16:34:55 | 显示全部楼层
  1. sentence = 'Today is a perfect day!'   # 输入英文短句如 Today is a perfect day!

  2. words = sentence.split()   # 按照空格分裂

  3. maxlength = 0
  4. xw = ''
  5. for word in words:
  6.     if not word . isalpha():
  7.         word = word[:-1]
  8.     if len(word) > maxlength:
  9.         maxlength = len(word)
  10.         xw = word

  11. print(xw)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-18 16:41:45 | 显示全部楼层
weiyideid823 发表于 2021-7-18 16:32
我改了下,然后结果把taday 和 perfect 都打印出来了,怎样改正我这个if else 的逻辑啊

当word比maxlength大的时候这个word就被放到list里,所以你打印list会打印两个,可以把最后的打印改为只打印list的最后一项
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-18 16:42:37 | 显示全部楼层
  1. sentence = 'today is a perfect day!'   # 输入英文短句如 Today is a perfect day!

  2. words = sentence.split()   # 按照空格分裂

  3. list =[]

  4. 另外我改动之后,最后运行出来的结果是把today 和 perfect 都打印出来了,看来我这if else 的逻辑有问题?

  5. maxlength = 0

  6. for word in words:
  7.     if word.isalpha():      #如果word 全为字母的话
  8.         if len(word) >= maxlength:
  9.             maxlength = len(word)
  10.             list.append(word)
  11.     elif len(word[:-1]) == maxlength:  # 排查文末有标点的特殊情况,去掉最后一个字符
  12.             list.append(word)

  13. print(list)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-18 16:43:21 | 显示全部楼层
可以elif
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-18 16:51:39 | 显示全部楼层

这个如果遇到最长的单词有多个的时候好像不行?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-18 16:56:04 | 显示全部楼层

这个运行出来好像也是同时打印的today 和 perfect啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-18 16:59:03 | 显示全部楼层


Today is a perfect dayyyyy!

如果句子是这个,好像不能同时把perfect 和dayyyyy 打印出来啊

另外 XW = ' '  是啥意思求教。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-18 17:17:28 | 显示全部楼层    本楼为最佳答案   
weiyideid823 发表于 2021-7-18 16:59
Today is a perfect dayyyyy!

如果句子是这个,好像不能同时把perfect 和dayyyyy 打印出来啊
  1. sentence = 'Today is a perfect day!'   # 输入英文短句如 Today is a perfect day!

  2. words = sentence.split()   # 按照空格分裂

  3. maxlength = 0
  4. for word in words:    # 确定最长单词的长度值
  5.     if not word . isalpha():
  6.         word = word[:-1]
  7.     if len(word) > maxlength:
  8.         maxlength = len(word)

  9. for word in words:    # 把符合最大长度的所有单词打印出来
  10.     if not word . isalpha():
  11.         word = word[:-1]
  12.     if len(word) == maxlength:
  13.         print(word)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-18 17:19:40 | 显示全部楼层
  1. sentence = 'Today is a perfect dayyyyy!'   # 输入英文短句如 Today is a perfect day!

  2. words = sentence.split()

  3. dic = {}

  4. maxlength = 0

  5. for word in words:
  6.     if not word.isalpha():
  7.         word = word[:-1]
  8.     length = len(word)
  9.     if length > maxlength:
  10.         maxlength = length
  11.     dic[word] = length


  12. for k,v in dic.items():
  13.     if v ==  maxlength:
  14.         print(k)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-18 17:47:53 | 显示全部楼层
本帖最后由 R0n9 于 2021-7-18 17:50 编辑

可否这样?
  1. from collections import Counter
  2. sentence = 'Today is a perfect day!'
  3. words = sentence.split()

  4. b = dict(Counter(words))
  5. c={key:value for key,value in b.items() if value > 1}
  6. if c:
  7.     print(max(zip(c.values(), c.keys())))
  8. else:
  9.     print('都是一个')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 14:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表