【python】字典处理

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

已知字符串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("按不重复单词长度降序排列的列表为:")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值