第一题:
aDict = {'1':'Mon','2':'Tue','3':'Wed','4':'Thu','5':'Fri',
'6':'Sat','7':'Sun'}
a = input()
b = aDict.get(a)
print(b)
第二题:
aDict = dict()
while(True):
s = input().split(':')
if s == ['']:
break
aDict[s[0]] = s[1]
s = input('请输入要查询的课程:\n')
try:
print(aDict[s])
except:
print('没有该门课程')
思考:列表下标的索引,split()返回序列!!!,抛出异常
第三题:
s1 = input().split()
s2 = input().split()
aDict = {}
for i in range(len(s1)):
aDict[s1[i]] = s2[i]
temp = sorted(aDict.keys())
result = []
for j in temp:
result = result + [(j,aDict[j])]
print(result)
第四题:
data = list(map(int,input().split(',')))
goal = int(input())
flag = False
for i in range(len(data)):
for j in range(i+1,len(data)):
if(data[i]+data[j] == goal):
print('{} {}'.format(i,j))
flag = True
if (flag == False) :
print('no answer')
第五题:待解决
oDict = eval(input())
tDict = eval(input())
one = set(oDict.keys())
two = set(tDict.keys())
one.update(two)
keys = sorted(one)
values = []
for i in keys:
temp = oDict.get(i)+tDict.get(i)
values.append(temp)
result = dict(zip(keys,values))
方式二:
oDict = eval(input())
temp = oDict.copy()
tDict = eval(input())
oDict.update(tDict)
for i in oDict:
if i in temp and i in tDict:
oDict[i] = temp[i] + oDict[i]
keys = list(oDict.keys())
keys.sort()
result = {}
for i in keys:
result[i] = oDict.get(i)
print(result)
第六题:
s = input()
aSet = set(s)
aDict = {}
for i in s:
aDict[i] = s.count(i)
aList = list(aDict.items())
aList.sort(key = lambda x:x[1],reverse = True)
for i in aList:
print('{} : {}'.format(i[0],i[1]))
思考:for i in s为了保证输出的原来序列,放入的时候按顺序放,因而先for i in aSet错误!!!