女朋友的Python练习题(集合,字典)
1、有一段英文新闻,请把每个单词的小写作为一个元素放到一个列表中。例如[“tropical”,“cyclones”]
Tropical cyclones and climate change concerns how tropical cyclones have changed, and are expected to further change due to climate change. The topic receives considerable attention from climate scientists who study the connections between storms and climate, and notably since 2005 makes news during active storm seasons. The 2018 U.S. National Climate Change Assessment reported that “increases in greenhouse gases and decreases in air pollution have contributed to increases in Atlantic hurricane activity since 1970.”
提示:转换英文大小写,去掉标点符号,去掉全数字(str.isdigit()为True时,表示字符串为全数字)
str1='''Tropical cyclones and climate change concerns how tropical cyclones have changed,
and are expected to further change due to climate change.
The topic receives considerable attention from climate scientists
who study the connections between storms and climate, and notably
since 2005 makes news during active storm seasons.
The 2018 U.S. National Climate Change Assessment reported that "increases in
greenhouse gases and decreases in air pollution have contributed to increases
in Atlantic hurricane activity since 1970."'''
str1.lower()
str1=str1.split()
str2=[]
for i in str1 :
if i.isdigit():
str1.pop()
else:
if i.find(',')>0:
j=i[:-1]
str2.append(j)
else:
str2.append(i)
print(str2)
2、某高校选举学生会主席,大家提名的候选人如下:
“陈启红”, “戚扬伙”, “韦炜熊”, “刘德耿”, “陈原庚”, “饶皋素”, “林必立”, “王卓谡”, “陈春映”, “卫蔽珠”, “施金筌”, “陈春映”, “何易晾”, “卫蔽珠”, “涂宣愈”, “韦品咏”, “韦品咏”, “卫蔽珠”, “赵票威”, “温曙映”, “韦品咏”, “何易晾”, “郑夜洲”, “卫蔽珠”, “黄准鲜”, “丘约靖”, “余键开”, “温曙映”, “韩侠胤”, “洪荐颢”, “王裁愿”, “周意竹”, “古军童”, “赵票威”, “余键开”, “韦炜熊”, “涂宣愈”, “韦炜熊”, “韦品咏”, “韦品咏”, “周谷岩”, “周意竹”, “林必立”, “尤三耀”, “韩侠胤”, “涂宣愈”, “余侠农”, “洪荐颢”, “黄准鲜”, “何易晾”, “丘约靖”, “余侠农”, “洪荐颢”, “赵毅敦”, “施金筌”, “邹度承”, “陈原庚”, “蔡保群”, “余键开”, “赵毅敦”, “洪荐颢”, “龙壬薇”, “周谷岩”, “王榕骋”, “周意竹”, “陈启红”, “古军童”, “邢琴嫣”, “周意竹”, “文伟嘉”, “黄准鲜”, “蔡保群”, “龙壬薇”, “韩侠胤”, “韩侠胤”, “何颖升”, “刘德耿”, “周意竹”, “涂宣愈”, “陈启红”, “戚扬伙”, “施金筌”, “余侠农”, “蔡保群”, “古军童”, “卫蔽珠”, “郑夜洲”, “胡俐庚”, “何颖升”, “何颖升”, “王卓谡”, “王裁愿”, “卫蔽珠”, “何颖升”, “余侠农”, “陈原庚”, “王榕骋”, “蔡保群”, “林必立”
请问有几个候选人?
提示:转换为集合用set()
list1=["陈启红", "戚扬伙", "韦炜熊", "刘德耿", "陈原庚", "饶皋素", "林必立", "王卓谡", "陈春映", "卫蔽珠", "施金筌", "陈春映", "何易晾", "卫蔽珠", "涂宣愈", "韦品咏", "韦品咏", "卫蔽珠", "赵票威", "温曙映", "韦品咏", "何易晾", "郑夜洲", "卫蔽珠", "黄准鲜", "丘约靖", "余键开", "温曙映", "韩侠胤", "洪荐颢", "王裁愿", "周意竹", "古军童", "赵票威", "余键开", "韦炜熊", "涂宣愈", "韦炜熊", "韦品咏", "韦品咏", "周谷岩", "周意竹", "林必立", "尤三耀", "韩侠胤", "涂宣愈", "余侠农", "洪荐颢", "黄准鲜", "何易晾", "丘约靖", "余侠农", "洪荐颢", "赵毅敦", "施金筌", "邹度承", "陈原庚", "蔡保群", "余键开", "赵毅敦", "洪荐颢", "龙壬薇", "周谷岩", "王榕骋", "周意竹", "陈启红", "古军童", "邢琴嫣", "周意竹", "文伟嘉", "黄准鲜", "蔡保群", "龙壬薇", "韩侠胤", "韩侠胤", "何颖升", "刘德耿", "周意竹", "涂宣愈", "陈启红", "戚扬伙", "施金筌", "余侠农", "蔡保群", "古军童", "卫蔽珠", "郑夜洲", "胡俐庚", "何颖升", "何颖升", "王卓谡", "王裁愿", "卫蔽珠", "何颖升", "余侠农", "陈原庚", "王榕骋", "蔡保群", "林必立"]
print(len(set(list1)))
3、输入一个字符串,输出字符串长度(排除重复字符),当输入-1时结束。
提示:转换为集合用set()
string=input()
while(string!='-1'):
print(len(set(string)))
string=input()
4、请用python做一个查询系统,输入星期x,输出它的英文。当输入-1时结束。
dict1={
'一':'Monday',
'二':'Tuesday',
'三':'Wednesday',
'四':'Thursday',
'五':'Friday',
'六':'Saturday',
'日':'Sunday'
}
weeks=input('星期')
while(weeks!='-1'):
print(dict1[weeks])
weeks=input('星期')
5、有一个中英字典,输入英文,显示相应的中文,当不存在时,显示None。
dict1={
'Monday':'星期一',
'Tuesday':'星期二',
'Wednesday':'星期三',
'Thursday':'星期四',
'Friday':'星期五',
'Saturday':'星期六',
'Sunday':'星期日'
}
zidian=input('请输入英文:(暂时只支持星期的查询)')
while(zidian!='-1'):
print(dict1.get(zidian))
zidian=input('请输入英文:')
6、2020年《财富》美国500强排行榜前3名:
排名 公司名称(中文) 营业收入(百万美元) 利润(百万美元)
1 沃尔玛 523,964 14,881
2 亚马逊 280,522 11,588
3 埃克森美孚 264,938 14,340
现在请用python做一个查询系统,输入公司名称可以查询到排名等信息。
提示:字典里的元素还是字典
dict1={
'沃尔玛':'1 523,964 14,881',
'亚马逊':'2 280,522 11,588',
'埃克森美孚':'3 264,938 14,340'
}
name=input("请输入公司名称:")
print('排名 营业收入(百万美元) 利润(百万美元)\n')
print(dict1[name])
7、建立一个字典,两个键:”奇数“,”偶数“。然后不停的输入整数,奇数放在”奇数“中,偶数放在”偶数“中。当输入-1时结束,输出”奇数“和”偶数“。
提示:遍历字典用
for k,v in dict.items():
print(k,v)
或者
for k in dict.keys():
print(k,dict[k])
dict1={
'奇数':[],
'偶数':[]
}
num=int(input('请输入一个数:'))
while(num!=-1):
if num%2==0:
dict1['偶数'].append(num)
else:
dict1['奇数'].append(num)
num=int(input('请输入一个数:'))
print('偶数',dict1['偶数'])
print('奇数',dict1['奇数'])