Python每日一练(牛客新题库)——第14天:元组、字典练习

文章目录

  • 1. 牛客运动会
  • 2. 遍历字典
  • 3. 毕业生就业调查
  • 4. 姓名与学号
  • 5. 如何让刷题更加高效呢?

前言

最近很多学了基础的小伙伴问我该怎么提升编程水平?学了基础该上哪刷题?明明学了很多,做项目却不知道怎么上手,其实这就是练得太少,只注重了学,却忽视了刷题,只有不断练习才能提高和巩固编程思维和能力!

在这里插入图片描述

刚好看到牛客网最近出了Python的新题库于是体验了一番感觉还不错

在这里插入图片描述

链接地址:牛客网 | Python从入门到实践四十招,废话少说速度上号,或者跟着下文一起刷题!!!

1. 牛客运动会

描述: 又到了一年一度的牛客运动会,Tom和Andy报名参加了项目,
但由于比赛前一天,Andy喝了太多碳酸饮料,导致身体不适,所以临时让Allen上场了,
换人参赛需要修改参赛名单,请完成以下内容模拟整个过程。

请创建一个依次包含字符串’Tom’和’Andy’的元组my_tuple,
先使用print()语句一行打印字符串’Here is the original tuple:’,再使用for循环将元组my_tuple的内容打印出来;

请使用try-except代码块执行语句my_tuple[1] = ‘Allen’,
若引发TypeError错误,先输出一个换行,再使用print()语句一行打印字符串"my_tuple[1] = ‘Allen’ cause cause a TypeError: ‘tuple’ object does not support item assignment";

再重新对my_tuple赋值一个新元组,新元组依次由字符串’Tom’和’Allen’构成。
输出一个换行,先使用print()语句一行打印字符串’The tuple was changed to:’,再使用for循环将元组my_tuple的内容打印出来,确定修改无误。

输入描述:无

输出描述:按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。
Here is the original tuple:
Tom
Andy

my_tuple[1] = ‘Allen’ cause a TypeError: ‘tuple’ object does not support item assignment

my_tuple was changed to:
Tom
Allen

实现代码:

my_tuple=('Tom', 'Andy')
print('Here is the original tuple:')
for x in my_tuple:
    print(x)
    
try:
    my_tuple[1]='Allen'
except TypeError:
    print()
    print("my_tuple[1] = 'Allen' cause a TypeError: 'tuple' object does not support item assignment")

my_tuple=('Tom', 'Allen')
print()
print('my_tuple was changed to:')
for x in my_tuple:
    print(x)

运行结果:

Here is the original tuple:
Tom
Andy

my_tuple[1] = 'Allen' cause a TypeError: 'tuple' object does not support item assignment

my_tuple was changed to:
Tom
Allen

2. 遍历字典

描述: 创建一个依次包含键-值对’<’: ‘less than’和’==’: ‘equal’的字典operators_dict,
先使用print()语句一行打印字符串’Here is the original dict:’,
再使用for循环遍历 已使用sorted()函数按升序进行临时排序的包含字典operators_dict的所有键的列表,使用print()语句一行输出类似字符串’Operator < means less than.‘的语句;

对字典operators_dict增加键-值对’>’: ‘greater than’后,
输出一个换行,再使用print()语句一行打印字符串’The dict was changed to:’,
再次使用for循环遍历 已使用sorted()函数按升序进行临时排序的包含字典operators_dict的所有键的列表,使用print()语句一行输出类似字符串’Operator < means less than.'的语句,确认字典operators_dict确实新增了一对键-值对。

输入描述:无

输出描述:按题目描述进行输出即可(注意前后两个输出部分需以一个空行进行分隔)。

实现代码:

operators_dict={'<':'less than','==':'equal'}
print('Here is the original dict:')
for k,v in sorted(operators_dict.items()):
    print(f'Operators {k} means {v}')
operators_dict['>']='greater than'
print()
print('The dict was changed to:')

for k,v in sorted(operators_dict.items()):
    print(f'Operators {k} means {v}')

运行结果:

Here is the original dict:
Operators < means less than
Operators == means equal

The dict was changed to:
Operators < means less than
Operators == means equal
Operators > means greater than

3. 毕业生就业调查

描述: 又到了毕业季,牛牛作为牛客大学的学生会主席,决定对本校的应届毕业生进行就业调查。
他创建了一个依次包含字符串’Niumei’、‘Niu Ke Le’、‘GURR’和’LOLO’的列表survey_list,作为调查名单;
又创建了一个依次包含键-值对’Niumei’: ‘Nowcoder’和’GURR’: 'HUAWEI’的字典result_dict,作为已记录的调查结果。
请遍历列表survey_list,如果遍历到的名字已出现在 包含字典result_dict的全部键的列表 里,
则使用print()语句一行输出类似字符串’Hi, Niumei! Thank you for participating in our graduation survey!'的语句以表达感谢,
否则使用print()语句一行输出类似字符串’Hi, Niu Ke Le! Could you take part in our graduation survey?'的语句以发出调查邀请。

输入描述:无

输出描述:按题目描述进行输出即可。
Hi, Niumei! Thank you for participating in our graduation survey!
Hi, Niu Ke Le! Could you take part in our graduation survey?
Hi, GURR! Thank you for participating in our graduation survey!
Hi, LOLO! Could you take part in our graduation survey?

实现代码:

survey_list = ['Niumei', 'Niu Ke Le', 'GURR', 'LOLO']
result_dict = {'Niumei': 'Nowcoder', 'GURR': 'HUAWEI'}

for i in survey_list:
    if i in result_dict.keys():
        print(f'Hi, {i}! Thank you for participating in our graduation survey!')
    else:
        print(f'Hi, {i}! Could you take part in our graduation survey?')

运行结果:

Hi, Niumei! Thank you for participating in our graduation survey!
Hi, Niu Ke Le! Could you take part in our graduation survey?
Hi, GURR! Thank you for participating in our graduation survey!
Hi, LOLO! Could you take part in our graduation survey?

4. 姓名与学号

描述
创建一个依次包含键-值对{‘name’: ‘Niuniu’和’Student ID’: 1}的字典my_dict_1,
创建一个依次包含键-值对{‘name’: ‘Niumei’和’Student ID’: 2}的字典my_dict_2,
创建一个依次包含键-值对{‘name’: ‘Niu Ke Le’和’Student ID’: 3}的字典my_dict_3,
创建一个空列表dict_list,使用append()方法依次将字典my_dict_1、my_dict_2和my_dict_3添加到dict_list里,
使用for循环遍历dict_list,对于遍历到的字典,使用print()语句一行输出类似字符串"Niuniu’s student id is 1."的语句以打印对应字典中的内容。

输入描述:无

输出描述:按照题意输出
Niuniu’s student id is 1.
Niumei’s student id is 2.
Niu Ke Le’s student id is 3.

代码实现:

my_dict_1 = {'name': 'Niuniu', 'Student ID': 1}
my_dict_2 = {'name': 'Niumei', 'Student ID': 2}
my_dict_3 = {'name': 'Niu Ke Le', 'Student ID': 3}
dict_list = []
dict_list.append(my_dict_1)
dict_list.append(my_dict_2)
dict_list.append(my_dict_3)

for i in dict_list:
    # 字典获取元素的方法i['key值'],或者i.get('key值')
    print(f"{i['name']}'s student id is {i.get('Student ID')}.")

运行结果:

Niuniu's student id is 1.
Niumei's student id is 2.
Niu Ke Le's student id is 3.

5. 如何让刷题更加高效呢?

嫌博主更新慢的小伙伴牛客网上号自行刷题

在这里插入图片描述

链接地址:牛客网 | Python从入门到实践四十招,废话少说速度上号!!!

你可能感兴趣的:(Python每日一练,python,算法,开发语言)