已知字符串s="When in the course of human events,it becomes necessary for one people to dissolve the political bands which have connected them with another,and to assume among the Powers of the earth,the separate and equal station to which the Laws of Nature and of Nature's God entitle them,a decent respect to the opinions of mankind requires that they should declare the causes.",编写程序,实现如下功能。
- 输出按不重复单词(不区分大小写)的长度的降序排列的列表。
- 生成一个新字典,字典的关键字是单词长度,字典的值是长度相同的单词放在一起组成的列表。输出新字典。
s=s.lower().replace(","," ").replace("."," ")
lst1=list(set(s.split()))
#print(lst1)
print("按不重复单词长度降序排列的列表为:")
lst1.sort(key=len,reverse=True)

该博客介绍了一段Python代码,用于处理字符串s并创建一个字典。首先,将字符串转换为小写并移除标点符号,然后分割成单词列表,通过集合获取不重复的单词,并按单词长度降序排序。接着,生成一个新字典,键为单词长度,值为相同长度的单词列表。最终输出这个按长度组织的字典。
最低0.47元/天 解锁文章
1927

被折叠的 条评论
为什么被折叠?



