pythonq期考练习题
目录
1. 九进制
dic={'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8}
a=input()
b=0
for i in range(len(a)):
b+=dic[a[i]]*(9**(len(a)-i-1))
print(b)
2. 旅游的城市
dic={ "张三风": ["北京", "成都"],
"李茉绸": ["上海", "广州", "兰州"],
"慕容福": ["太原", "西安", "济南", "上海"]}
for k,v in dic.items():
print("{}去过{}个城市".format(k,len(v)))
ls=[]
for k,v in dic.items():
if "上海" in v:
ls.append(k)
print("去过上海的有{}人,他们是{}".format(len(ls),'、'.join(ls)))
3. 年终奖
lst_staff = ["李梅", "张富", "付妍", "赵诺", "刘江"]
dic_award={"张富":10000, "赵诺":15000}
for i in lst_staff:
award=dic_award.get(i,5000)
print(f"{i} {award}")
print(dic_award)
4. 成绩单
dic_score = {
'徐丽': [88, 90, 98, 95],
'张兴': [85, 92, 95, 98],
'刘宁': [89, 89, 90, 92],
'张旭': [82, 86, 89, 90]
}
for stu,score in dic_score.items():
avg=sum(score)/len(score)
score.append(avg)
dic_score[stu]=score
for stu,score in dic_score.items():
print(stu,score)
5. 统计单词次数
好像上次也有这题
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 which impel them to the separation. "
s=s.replace(',','')
s=s.replace('.','')
s=s.lower()
ls=s.split()
dic={}
for i in ls:
dic[i]=dic.get(i,0)+1
print(dic)
ls1=list(dic.items())
ls1.sort(key=lambda x:x[1],reverse=True)
for i in range(5):
print(ls1[i][0])
6. 公交车
lst_busstop = ["龙江新城市", "阳光广场", "汉江路", "嫩江路", "清凉山公园", "拉萨路", "五台山", "莫愁路"] dic_estate ={ "龙江新城市": ["白云园","腾飞园"], "阳光广场": ["龙江小区","芳草园"], "汉江路": ["金信花园","龙凤花园"], "嫩江路": ["西城蓝湾","花开四季"] } start=input() end=input() start_stop=None end_stop=None for i in lst_busstop: if start in dic_estate.get(i,[]): '''查询起始小区在哪一站,若查询到相应站则赋值给start_stop,若未查询到则返回空列表''' start_stop=i for i in lst_busstop: if end in dic_estate.get(i,[]): #同上 end_stop=i if lst_busstop.index(start_stop)<lst_busstop.index(end_stop): #比较大小,判断用正还是反描述,以及方便用大值-小值 result="需要正方向坐{}站".format(lst_busstop.index(end_stop)-lst_busstop.index(start_stop)) else: result="需要反方向坐{}站".format(lst_busstop.index(start_stop)-lst_busstop.index(end_stop)) print(start_stop,end_stop,result)
Tips: 在 Python 中,空列表、空字符串、空字典、空集合、0、None 和 False 都被认为是“假值”(falsy)。这意味着在布尔上下文中,它们会被评估为 False, 故当返回空列表时if为false,后续语句不执行
7. 集合练习
ls=eval(input())
k=eval(input())
ls.sort() #排序
print(ls)
set1=set(ls)
ls1=list(set1) #set无序,没有索引,转换回列表方便输出
print(ls1[k-1]) #记得下标-1
8. 歌唱比赛
set1={1,2,3,4,5}
set2={6,7,8,9,10}
set3={4,7,9,1,2,2,6,2,2,1,6,9,7,4,5,5,7,9,5,5,4}
'''set3这里我本来想用字符串方法去除顿号然后转为数字,但最后好像没成功,只好手打了'''
'''重新尝试后找出了不手打的方法,但较为繁琐
s='4、7、9、1、2、2、6、2、2、1、6、9、7、4、5、5、7、9、5、5、4'
ls=s.split('、')
set3=set(ls)
set3={int(item) for item in set3}
'''
print(set3)
print(set1 & set3)
print(set2 - set3)
set2.add(11)
r=eval(input())
if r in set3:
print("是")
else:
print("否")