目录
7-1 输出星期名缩写
box = {1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri', 6: 'Sat', 7: 'Sun'}
key = int(input())
print(box[key])
7-2 图的字典表示
N = int(input())
roadNum = 0
sumLength = 0
for index in range(0, N):
dict1 = eval(input()) # 读取第一层字典
for z in dict1:
dict2 = dict1[z] # 读取第二层字典
for z1 in dict2: # 遍历字典
# print("%c %d" % (z1,dict2[z1]))
sumLength += dict2[z1]
roadNum += 1
print("%d %d %d" % (N, roadNum, sumLength))
7-3 四则运算(用字典实现)
dict1 = {'+': 'a+b', '-': 'a-b', '*': 'a*b', '/': 'a/b'}
a = int(input())
op = input()
b = int(input())
if op == '/' and b == 0:
print("divided by zero")
else:
result = eval(dict1[op])
print("%.2f" % result)
7-4 分析活动投票情况
nums = [int(x) for x in input().split(',')]
ticket = [0 for i in range(0, 11)]
for x in nums:
ticket[x] += 1
output = 0
for index in range(6, 11):
if ticket[index] == 0:
if output == 1:
print(' ', end='')
output = 1
print(index, end='')
7-5 统计字符出现次数
str1 = input()
key = input()
result = 0
for index in str1:
if index == key:
result += 1
print(result)
7-6 统计工龄
N = int(input())
key = [0 for z in range(0, 51)]
nums = [int(x) for x in input().split()]
for index in nums:
key[index] += 1
for index in range(0, 51):
if key[index] > 0:
print("%d:%d" % (index, key[index]))
7-7 列表去重
list1 = eval(input())
dict1 = {}
for index in list1:
if index not in dict1:
dict1[index] = 0
output = 0
for index in dict1:
if output == 1:
print(' ', end='')
print(index, end='')
output = 1
7-8 能被3,5和7整除的数的个数(用集合实现)
n, m = map(int, input().split())
print(int(m/105)-int(n/105))
a, b = map(int, input().split())
f3 = set()
f5 = set()
f7 = set()
for num in range(a, b + 1):
if num % 3 == 0:
f3.add(num)
if num % 5 == 0:
f5.add(num)
if num % 7 == 0:
f7.add(num)
print(len(f3 & f5 & f7))
7-9 求矩阵鞍点的个数
n = int(input())
box = []
for z in range(0, n):
line = [int(x) for x in input().split()]
box.append(line)
# 找到改行最大的数
def lineMAX(lineNum):
maxNum = 0
for index1 in range(1, n):
if box[lineNum][index1] > box[lineNum][maxNum]:
maxNum = index1
return box[lineNum][maxNum]
# 找到该列最小的数
def rankMin(rankNum):
minNum = 0
for index2 in range(1, n):
if box[index2][rankNum] < box[minNum][rankNum]:
minNum = index2
return box[minNum][rankNum]
result = 0
for z1 in range(0, n):
for z2 in range(0, n):
if box[z1][z2] == lineMAX(z1) == rankMin(z2):
result += 1
print(result)
7-10 两数之和
nums = [int(x) for x in input().split(',')]
key = int(input())
flag = 0
for z in range(0,len(nums)):
for z1 in range(z,len(nums)):
if nums[z] + nums[z1] == key:
print("%d %d" % (z,z1))
flag = 1
if flag == 0:
print("no answer")
7-11 字典合并
dict1 = eval(input())
dict2 = eval(input())
for index in dict2:
if index in dict1:
dict1[index] += dict2[index]
else:
dict1[index] = dict2[index]
result = sorted(dict1.items(), key=lambda item: item[0] if type(item[0]) == int else ord(item[0]))
print(str(dict(result)).replace(' ', '').replace("'", '"'))
# 网上找到的说明,说实话这题如果用队列+字典模拟这个过程实在是太麻烦了
# for key,value in b.items():
# a[key] = a.get(key,0)+value
# #get方法语法:dict.get(key,default=None),key-字典中要查找的值,default-如果指定键的值不存在时,返回该默认值
# d = sorted(a.items(),key = lambda item:item[0] if type(item[0])==int else ord(item[0]))
# #lambda是匿名函数,lambda函数实现的主要功能是:如果是数字正常比较合并即可,如果是字母的话要转ascii码值然后再比较
# out = str(dict(d)).replace(' ','').replace("'",'"')
# #将得到的字典d按照指定格式进行改造
# print(out)