第一次考试(基础部分)

时间:2022-06-04 20:55:00
# lis = [['哇',['how',{'good':['am',100,'99']},60],'I']]
# # 1)将列表lis中的'am'变成大写。(1分)
# # lis[0][1][1]['good'][0] = lis[0][1][1]['good'][0].upper()
#
# # 2)将列表中的100通过数字相加在转换成字符串的方式变成'10010'。(1分)
# # lis[0][1][1]['good'][1] = str(lis[0][1][1]['good'][1] + 9910)
#
# # 3)将列表中的字符串'99'通过字符串替换的方式变成'6666'。(1分)
# # lis[0][1][1]['good'][2] = lis[0][1][1]['good'][2].replace("9","66")
# print(lis)



# dic = {'k1':'v1','k2':['alex','sb'],(1,2,3,):{'k3':['2',100,'wer']}}
# # 1)将'k3'对应的值的最后面添加一个元素'23'。(1分)
# dic[(1,2,3,)]['k3'].append("23")
# # 2)将'k2'对应的值的第0个位置插入元素'a'。(1分)
# dic['k2'].insert(0, "a")
# # 3)将(1,2,3,)对应的值添加一个键值对'k4','v4'。(1分)
# dic[(1,2,3,)]['k4'] = 'v4'
# print(dic)


# 使用range打印100,99,98,....1,0
# for i in range(100, -1, -1):
# print(i)



# 请用户输入一个数n, 判断用户输入的数字是否是质数.(5分)
# n = int(input("请输入一个数字"))
# for i in range(2, n):
# if n % i == 0:
# print("不是一个质数")
# break
# else:
# print("是一个质数")


#
# 2、有如下演员和出场费的字典, dic = {'周杰伦': 8000, '林俊杰': 5000, '太白': 5, 'alex': 5},
# 请删除掉出场费低于平均值的演员(5分)
# dic = {'周杰伦': 8000, '林俊杰': 5000, '太白': 5, 'alex': 5}
# sum = 0
# for v in dic.values():
# sum += v
#
# avg = sum / len(dic)
#
# lst = []
#
# for k, v in dic.items():
# if v < avg:
# lst.append(k) # 把要删除的内容保存
#
# for el in lst: # 循环列表
# dic.pop(el)
#
# print(dic)




#
# 3、有文件t1.txt里面的内容为:(5分)
# id, name, age, phone, job
# 1, alex, 22, 13651054608, IT
# 2, wusir, 23, 13304320533, Tearcher
# 3, taibai, 18, 1333235322, IT
#
# 利用文件操作,将其构造成如下数据类型。
# [{'id': '1', 'name': 'alex', 'age': '22', 'phone': '13651054608', 'job': 'IT'}, ......]
#
# f = open("t1.txt", mode="r", encoding="utf-8")
# title = f.readline().strip() # 标题
# titleLst = title.split(",")
# result = []
# for line in f: # 数据
# dic = {}
# line = line.strip()
# dataLst = line.split(",")
# for i in range(len(titleLst)):
# dic[titleLst[i]] = dataLst[i]
#
# result.append(dic)
# print(result)



# 4、开发敏感词语过滤程序,提示用户不停的输入评论内容,
# 如果用户输入的内容中包含特殊的字符则将用户输入的内容中的敏感词替换成等长度的 *(苍老师替换成 ** *),
# 并添加到列表中,如果用户输入的内容中没有敏感词汇则直接添加到列表中,
# 最终将列表中的全部内容进行打印
# 敏感词列表:li = ['苍老师', '东京热', '武藤兰', '波多野结衣', 'alex'](5
# 分)
# li = ['苍老师', '东京热', '武藤兰', '波多野结衣', 'alex']
# result = []
# while 1:
# content = input("请输入你要评论的内容(输入Q退出)")
# if content.upper() == "Q":
# break
# for ci in li:
# if ci in content:
# content = content.replace(ci, "*"*len(ci))
#
# result.append(content)
#
# print(result)
#




#
# 5、让用户输入10个数,将这10个数保存在列表中,然后将所有大于55
# 的值保存至字典的第一个key的值中,将小于等于55的值保存至第二个key的值中。(6分)
# lst = []
# for i in range(10):
# num = int(input("请输入数字:"))
# lst.append(num)
#
# print(lst)
#
# dic = {}
# for el in lst: # 每一个数字
# if el > 55:
# # setdefault(key, value)
# # 判断key是否存在. 如果存在. 根据key把结果查询出来
# # 如果key不存在. 首先新增. key-> value添加到字典., 然后再根据key把value查询出来
# dic.setdefault("key1", []).append(el)
# else:
# dic.setdefault("key2", []).append(el)
#
# print(dic)


#
# 6、车牌区域划分,给出一下车牌和地点信息对照,请根据车牌信息,分析出各省的车牌持有数量。
# cars = ['鲁A32444', '鲁B12333', '京B8989M', '黑C49678', '黑C46555', '沪B25041', '黑C34567']
# locations = {'沪': '上海', '京': '北京', '黑': '黑龙江', '鲁': '山东', '鄂': '湖北', '湘': '湖南'}(6
# 分)

cars = ['鲁A32444', '鲁B12333', '京B8989M', '黑C49678', '黑C46555', '沪B25041', '黑C34567']
locations = {'沪': '上海', '京': '北京', '黑': '黑龙江', '鲁': '山东', '鄂': '湖北', '湘': '湖南'}

# dic = {}
# for c in cars: # c是车牌
# shou = c[0] # 鲁
# sheng = locations[shou] # 山东省
# if dic.get(sheng)== None:
# dic[sheng] = 1
#
# else:
# dic[sheng] += 1
# print(dic)


# dic = {}
# for c in cars: # c是车牌
# dic[locations[c[0]]] = dic.setdefault(locations[c[0]], 0) + 1
# print(dic)



# 7、按要求完成下列转化。(8分)
# list3 = [
# {"name": "alex", "hobby": "抽烟"},
# {"name": "alex", "hobby": "喝酒"},
# {"name": "alex", "hobby": "烫头"},
# {"name": "alex", "hobby": "Massage"},
# {"name": "wusir", "hobby": "喊麦"},
# {"name": "wusir", "hobby": "街舞"},
# {"name": "taibai", "hobby": "睡觉"},
# ]
# list4 = [
# {"name": "alex", "hobby_list": ["抽烟", "喝酒", "烫头", "Massage"]},
# {"name": "wusir", "hobby_list": ["喊麦", "街舞"]},
# {"name": "taibai", "hobby_list": ["喊麦"]},
# ]
# 将list3
# 这种数据类型转化成list4类型, 你写的代码必须支持可拓展,
# 比如list3
# 数据在加一个这样的字典
# {"name": "wusir", "hobby": "溜达"}, 你的list4
# {"name": "wusir", "hobby_list": ["喊麦", "街舞", "溜达"],
# 或者list3增加一个字典{"name": "太白", "hobby": "开车"},
# 你的list4{"name": "太白", "hobby_list": ["开车"], 无论按照要求加多少数据, 你的代码都可以转化.如果不支持拓展, 则4分, 支持拓展则8分.


list3 = [
{"name": "alex", "hobby": "抽烟"},
{"name": "alex", "hobby": "喝酒"},
{"name": "alex", "hobby": "烫头"},
{"name": "alex", "hobby": "Massage"},
{"name": "wusir", "hobby": "喊麦"},
{"name": "wusir", "hobby": "街舞"},
{"name": "宝宝", "hobby": "看电影"},
{"name": "宝宝", "hobby": "学习"},
{"name": "宝宝", "hobby": "python"},
{"name": "宝宝", "hobby": "热衷于编码"},
]

list4 = []

for el in list3: # {"name": "alex", "hobby": "喝酒"},
for el2 in list4:
if el['name'] == el2['name']:
el2['hobby_list'].append(el['hobby'])
break
else:
list4.append({"name": el['name'], "hobby_list":[el['hobby']]})

print(list4)