Kayko 发表于 2021-7-17 06:57:50

{:10_257:}

kaohsing 发表于 2021-7-17 07:32:42

#!/usr/bin/python
import re
s='''Hmm~There's a heroIf you look inside your heartYou don't have to be afraid
Of what you are There's an answerIf you reach into your soul
And the sorrow that you knowWill melt awayAnd then a hero comes along
With the strength to carry onAnd you cast your fears aside
And you know you can surviveSo when you feel like hope is gone
Look inside you and be strongAnd you'll finally see the truth
That a hero lies in you   abcdeabcde It's a long roadWhen you face the world alone
No one reaches out a handor you to holdYou can find love
If you search within yourself And the emptiness you felt will disappear
And then a hero comes along With the strength to carry onAnd you cast your fears aside
And you know you can survive So when you feel like hope is gone
Look inside you and be strongAnd you'll finally see the truth
That a hero lies in youLord knows......Dreams are hard to follow

'''

def k(s):
        result=""
        l=list( set(re.findall(r'(\w{2,})', s)))       
        z=sorted(l,key=lambda l: len(l))
        result=z[-1]
        a=len(z.pop())       
        for i in z[::-1]:               
                if (len(i)==a):                       
                        result=result+","+i
                else:
                        return result       
print(k(s))

kaohsing 发表于 2021-7-17 09:41:54

#!/usr/bin/python
import re
s='''Hmm~There's a heroIf you look inside your heartYou don't have to be afraid
Of what you are There's an answerIf you reach into your soul
And the sorrow that you knowWill melt awayAnd then a hero comes along
With the strength to carry onAnd you cast your fears aside
And you know you can surviveSo when you feel like hope is gone
Look inside you and be strongAnd you'll finally see the truth
That a hero lies in you   abcdeabcde It's a long roadWhen you face the world alone
No one reaches out a handor you to holdYou can find love
If you search within yourself And the emptiness you felt will disappear
And then a hero comes along With the strength to carry onAnd you cast your fears aside
And you know you can survive So when you feel like hope is gone
Look inside you and be strongAnd you'll finally see the truth
That a hero lies in youLord knows......Dreams are hard to follow abcdeabcde

'''

def k(m):
        global i
        z=len(m)
        if(z>=i):               
                if(z>i):
                        l.clear()
                i=z
                l.append(m)
       
def kao(s):        
        re.sub('(\w+)', k, s)
        return l       
i=1
l=[]       
print(kao(s))

Twilight6 发表于 2021-7-17 11:43:05

青出于蓝 发表于 2021-7-15 22:07
遇到问题要多思考
上边代码最重要的是split函数,将字符串分割



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

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

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

青出于蓝 发表于 2021-7-17 11:54:06

Twilight6 发表于 2021-7-17 11:43
这个字符:'I Love FishC! I Love Python! I Love Window!'

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


哦吼
原来是这样.................................................

weiyideid823 发表于 2021-7-17 17:46:44

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

我也讨厌这种人啊,但是由于各种原因我就成了这种人,你要是喷了之后还有时间的话可以看看我早期的帖子都是有自己的想法的,不过没时间也无所谓啦

0HB 发表于 2021-7-18 02:45:44

本帖最后由 0HB 于 2021-7-19 01:10 编辑

Sr = input('请输入句子:')
list1 = Sr.split()
list2 =
for x in list1:
    if list1.count(x) < max(list2):
      pass
    elif list1.count(x) == max(list2):
      list3.append('出现最多次的单词:' + x + '共{0}次'.format(list1.count(x)))
    else:
      list3 = ['出现最多次的单词:' + x + '共{0}次'.format(list1.count(x))]
    list2.append(list1.count(x))
print(set(list3))

(后补充:不好意思,看错需求了,以为是出现最多次的单词.... )

0HB 发表于 2021-7-18 02:50:14

本帖最后由 0HB 于 2021-7-18 03:00 编辑

0HB 发表于 2021-7-18 02:45
Sr = input('请输入句子:')
list1 = Sr.split()
list2 =


希望有帮助。用的知识都很基础{:5_105:} (后补充:不好意思,看错需求了,以为是出现最多次的单词....{:5_100:} )

0HB 发表于 2021-7-18 03:09:02

本帖最后由 0HB 于 2021-7-19 01:11 编辑

Sr = input('请输入句子:')
list1 = Sr.split()                                                                         # 以空格为分割线,恰好满足单词的划分!
list2 = # 建立一个列表,辅助比较长度!
for x in list1:
    if len(x) < max(list2):                                                             # 小了,无事发生
      pass
    elif len(x) == max(list2):
      list3.append('最长的单词:' + x + '共{0}个字母'.format(len(x)))                     # 有两个长度相等的最长单词时,把它们都记下来
    else:
      list3 = ['最长的单词:' + x + '共{0}个字母'.format(len(x))]                         # 更长的出现时,把前面记录的删去并记下更长的!
    list2.append(len(x))                                                                           # 记下每个单词的长度,下次循环要参与比较
print(set(list3))                              # 因为for的遍历,可能会有让 出现多次的 同一单词 重复记录在list3中。而用set()改为集合,则删去重复的单词
页: 1 [2]
查看完整版本: 求住以下python 实现代码