鱼C论坛

 找回密码
 立即注册
查看: 4417|回复: 28

[已解决]求住以下python 实现代码

[复制链接]
发表于 2021-7-15 21:26:34 | 显示全部楼层 |阅读模式

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

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

x
菜鸟求助大神以下python 实现代码啊。。。



从一个英文短句中找出字符最多的那个单词
最佳答案
2021-7-15 21:26:35

参考代码:

  1. # 你的字符串
  2. string = 'I Love FishC! I Love Python! I Love Window!'

  3. # 将字符串进行切割,分隔出单词后有些单词末尾会有字符,在用字符串方法切割掉最后一个字符
  4. # 用 set 将单词合并同样的单词,转会 list 用于下面的排序
  5. words = list(set([i if i.isalpha() else i[:-1] for i in string.split()]))

  6. # 这里 sort 按照单词长度排序,且由长到短
  7. words.sort(key=lambda x:len(x), reverse=True)

  8. # for 循环打印 filter 筛选字符串长度都和 第一个字符串长度相同的字符,均为最长单词
  9. for i in filter(lambda x:len(x) == len(words[0]), words):
  10.     print(i, end=' ')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-18 03:09:02 | 显示全部楼层
本帖最后由 0HB 于 2021-7-19 01:11 编辑
  1. Sr = input('请输入句子:')
  2. list1 = Sr.split()                                                                         # 以空格为分割线,恰好满足单词的划分!
  3. list2 = [0]  # 建立一个列表,辅助比较长度!
  4. for x in list1:
  5.     if len(x) < max(list2):                                                             # 小了,无事发生
  6.         pass
  7.     elif len(x) == max(list2):
  8.         list3.append('最长的单词:' + x + '共{0}个字母'.format(len(x)))                       # 有两个长度相等的最长单词时,把它们都记下来
  9.     else:
  10.         list3 = ['最长的单词:' + x + '共{0}个字母'.format(len(x))]                         # 更长的出现时,把前面记录的删去并记下更长的!
  11.     list2.append(len(x))                                                                           # 记下每个单词的长度,下次循环要参与比较
  12. print(set(list3))                              # 因为for的遍历,可能会有让 出现多次的 同一单词 重复记录在list3中。而用set()改为集合,则删去重复的单词
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-18 02:50:14 | 显示全部楼层
本帖最后由 0HB 于 2021-7-18 03:00 编辑
0HB 发表于 2021-7-18 02:45
Sr = input('请输入句子:')
list1 = Sr.split()
list2 = [0]


希望有帮助。用的知识都很基础 (后补充:不好意思,看错需求了,以为是出现最多次的单词....
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-18 02:45:44 | 显示全部楼层
本帖最后由 0HB 于 2021-7-19 01:10 编辑
  1. Sr = input('请输入句子:')
  2. list1 = Sr.split()
  3. list2 = [0]
  4. for x in list1:
  5.     if list1.count(x) < max(list2):
  6.         pass
  7.     elif list1.count(x) == max(list2):
  8.         list3.append('出现最多次的单词:' + x + '共{0}次'.format(list1.count(x)))
  9.     else:
  10.         list3 = ['出现最多次的单词:' + x + '共{0}次'.format(list1.count(x))]
  11.     list2.append(list1.count(x))  
  12. print(set(list3))
复制代码

(后补充:不好意思,看错需求了,以为是出现最多次的单词.... )
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-17 17:46:44 | 显示全部楼层
聂嘉辉 发表于 2021-7-16 22:39
最讨厌这种不劳而获的。你一个代码不写,有些人太善良了,你遇到问题请教一下没什么,但是什么都没有就很可 ...

我也讨厌这种人啊,但是由于各种原因我就成了这种人,你要是喷了之后还有时间的话可以看看我早期的帖子都是有自己的想法的,不过没时间也无所谓啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-17 11:54:06 | 显示全部楼层
Twilight6 发表于 2021-7-17 11:43
这个字符:'I Love FishC! I Love Python! I Love Window!'

自己测试下代码,字符都没去掉

哦吼
原来是这样.................................................
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-17 11:43:05 | 显示全部楼层
青出于蓝 发表于 2021-7-15 22:07
遇到问题要多思考
上边代码最重要的是split函数,将字符串分割



这个字符:'I Love FishC! I Love Python! I Love Window!'

自己测试下代码,字符都没去掉

没过 7 天别那么着急的举报得最佳,是你的最佳都会是你的哈

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-17 09:41:54 | 显示全部楼层
  1. #!/usr/bin/python
  2. import re
  3. s='''Hmm~  There's a hero  If you look inside your heart  You don't have to be afraid
  4. Of what you are There's an answer  If you reach into your soul
  5. And the sorrow that you know  Will melt away  And then a hero comes along
  6. With the strength to carry on  And you cast your fears aside
  7. And you know you can survive  So when you feel like hope is gone
  8. Look inside you and be strong  And you'll finally see the truth
  9. That a hero lies in you   abcdeabcde It's a long road  When you face the world alone
  10. No one reaches out a hand  or you to hold  You can find love
  11. If you search within yourself And the emptiness you felt will disappear
  12. And then a hero comes along With the strength to carry on  And you cast your fears aside
  13. And you know you can survive So when you feel like hope is gone
  14. Look inside you and be strong  And you'll finally see the truth
  15. That a hero lies in you  Lord knows......Dreams are hard to follow abcdeabcde

  16. '''

  17. def k(m):
  18.         global i
  19.         z=len(m[0])
  20.         if(z>=i):               
  21.                 if(z>i):
  22.                         l.clear()
  23.                 i=z
  24.                 l.append(m[0])
  25.        
  26. def kao(s):        
  27.         re.sub('(\w+)', k, s)
  28.         return l       
  29. i=1
  30. l=[]       
  31. print(kao(s))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-17 07:32:42 | 显示全部楼层
  1. #!/usr/bin/python
  2. import re
  3. s='''Hmm~  There's a hero  If you look inside your heart  You don't have to be afraid
  4. Of what you are There's an answer  If you reach into your soul
  5. And the sorrow that you know  Will melt away  And then a hero comes along
  6. With the strength to carry on  And you cast your fears aside
  7. And you know you can survive  So when you feel like hope is gone
  8. Look inside you and be strong  And you'll finally see the truth
  9. That a hero lies in you   abcdeabcde It's a long road  When you face the world alone
  10. No one reaches out a hand  or you to hold  You can find love
  11. If you search within yourself And the emptiness you felt will disappear
  12. And then a hero comes along With the strength to carry on  And you cast your fears aside
  13. And you know you can survive So when you feel like hope is gone
  14. Look inside you and be strong  And you'll finally see the truth
  15. That a hero lies in you  Lord knows......Dreams are hard to follow

  16. '''

  17. def k(s):
  18.         result=""
  19.         l=list( set(re.findall(r'(\w{2,})', s)))       
  20.         z=sorted(l,key=lambda l: len(l))
  21.         result=z[-1]
  22.         a=len(z.pop())       
  23.         for i in z[::-1]:               
  24.                 if (len(i)==a):                       
  25.                         result=result+","+i
  26.                 else:
  27.                         return result       
  28. print(k(s))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-17 06:57:50 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-17 04:35:43 | 显示全部楼层
  1. # 找出一句话中最长的单词
  2. def longest_word(sentence: str) -> str:
  3.     word_list = sentence.split()
  4.     m = ''
  5.     for w in word_list:
  6.         if len(w) > len(m):
  7.             m = w
  8.     return m
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-16 22:39:38 | 显示全部楼层
最讨厌这种不劳而获的。你一个代码不写,有些人太善良了,你遇到问题请教一下没什么,但是什么都没有就很可恶了。你这样能学到什么呢?如果你没学过就不要做题!先学会。

评分

参与人数 1鱼币 +2 收起 理由
青出于蓝 + 2 +1

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-16 17:35:30 | 显示全部楼层
这解法越来越离谱了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-16 14:07:59 | 显示全部楼层
hrp 发表于 2021-7-16 10:36
如果有多个单词是一样长且都是句子中最长的单词呢?是全部输出?还是输出最后一个?或者是第一个?

我理解的应该是全部输出
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-16 10:36:56 From FishC Mobile | 显示全部楼层
如果有多个单词是一样长且都是句子中最长的单词呢?是全部输出?还是输出最后一个?或者是第一个?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-16 10:09:15 | 显示全部楼层
  1. import re

  2. RE_WORD = '\w+'
  3. def DealFile(filename):
  4.     # 读取文件信息
  5.     with open(filename) as f:
  6.         data = f.readlines()
  7.    
  8.     # 单词出现次数统计字典{'word':次数}
  9.     word_dict = {}
  10.     # 处理每一行字符串
  11.     for line in data:
  12.         # 正则表达式获取一行中所有单词
  13.         words = re.findall(RE_WORD, line)
  14.         # 将单词加入到统计字典中
  15.         for w in words:
  16.             # 除空格
  17.             w = w.strip()
  18.             # 出现次数+1
  19.             word_dict[w] = word_dict.get(w,0) + 1
  20.             
  21.     # 按单词次数排序, [(word, count),...]
  22.     word_count_list = sorted(word_dict.items(), key=lambda x: x[1], reverse=True)
  23.     # 打印次数最多的单词
  24.     maxVal = word_count_list[0][1]
  25.     for w, count in word_count_list:
  26.         if count == maxVal:
  27.             print(w)
  28.         else:
  29.             break
  30.         
  31. DealFile('test.txt')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-16 09:24:42 | 显示全部楼层
weiyideid823 发表于 2021-7-15 23:15
我运行了以下,打印出来的是最开始的句子呀

没有错啊, 是最长的单词,
PS: 这个程序要排除一下标点符号
屏幕截图 2021-07-16 092249.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-15 21:26:35 | 显示全部楼层    本楼为最佳答案   

参考代码:

  1. # 你的字符串
  2. string = 'I Love FishC! I Love Python! I Love Window!'

  3. # 将字符串进行切割,分隔出单词后有些单词末尾会有字符,在用字符串方法切割掉最后一个字符
  4. # 用 set 将单词合并同样的单词,转会 list 用于下面的排序
  5. words = list(set([i if i.isalpha() else i[:-1] for i in string.split()]))

  6. # 这里 sort 按照单词长度排序,且由长到短
  7. words.sort(key=lambda x:len(x), reverse=True)

  8. # for 循环打印 filter 筛选字符串长度都和 第一个字符串长度相同的字符,均为最长单词
  9. for i in filter(lambda x:len(x) == len(words[0]), words):
  10.     print(i, end=' ')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-16 08:29:29 | 显示全部楼层
本帖最后由 3236654291 于 2021-7-16 08:32 编辑

啊,看错了
你要求的是单词
我看成了字母

没事你们继续忙
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-16 08:23:21 | 显示全部楼层
如果要靠学过的知识就看那个人说的就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-30 04:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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