牛客题霸在线编程Python题库——Python入门到实践40招(三)列表

简介: 牛客题霸在线编程Python题库——Python入门到实践40招(三)列表

8.发送offer(入门)


某公司在面试结束后,创建了一个依次包含字符串 'Allen' 和 'Tom' 的列表offer_list,作为通过面试的名单。


请你依次对列表中的名字发送类似 'Allen, you have passed our interview and will soon become a member of our company.' 的句子。


但由于Tom有了其他的选择,没有确认这个offer,HR选择了正好能够确认这个offer的Andy,所以请把列表offer_list中 'Tom' 的名字换成 'Andy' ,


再依次发送类似 'Andy, welcome to join us!' 的句子。


offer_list = ['Allen', 'Tom']
for i in offer_list:
    print(f'{i}, you have passed our interview and will soon become a member of our company.')
offer_list[1] = 'Andy'
for j in offer_list:
    print(f'{j}, welcome to join us!')


运行


Allen, you have passed our interview and will soon become a member of our company.
Tom, you have passed our interview and will soon become a member of our company.
Allen, welcome to join us!
Andy, welcome to join us!


9.派对名单(中等)


为庆祝驼瑞驰在牛爱网找到合适的对象,所以驼瑞驰创建了一个依次包含字符串 'Niuniu' 和 'Niu Ke Le' 的列表guest_list,作为庆祝派对的邀请名单。


请你依次对列表中的名字发送类似'Niuniu, do you want to come to my celebration party?'的句子。


驼瑞驰的好朋友牛牛、GURR哥和LOLO姐也正好有空,所以请使用insert()方法把字符串'GURR'插入到列表guest_list的开头,


再使用insert()方法把字符串'Niumei'插入到字符串'Niu Ke Le'的前面,再使用append()方法把字符串'LOLO'插入到列表guest_list的最后,


再依次发送类似'Niuniu, thank you for coming to my celebration party!'的句子。


guest_list = ['Niuniu', 'Niu Ke Le']
for i in guest_list:
    print(f'{i}, do you want to come to my celebration party?')
print()
guest_list.insert(0, 'GURR')
guest_list.insert(2, 'Niumei')
guest_list.append('LOLO')
for j in guest_list:
    print(f'{j}, thank you for coming to my celebration party!')


运行


Niuniu, do you want to come to my celebration party?
Niu Ke Le, do you want to come to my celebration party?
GURR, thank you for coming to my celebration party!
Niuniu, thank you for coming to my celebration party!
Niumei, thank you for coming to my celebration party!
Niu Ke Le, thank you for coming to my celebration party!
LOLO, thank you for coming to my celebration party!


10.投递简历(入门)


company_list = ['Alibaba', 'Baidu', 'Tencent', 'MeiTuan', 'JD']
for i in company_list:
    print(f'Hello {i}, here is my resume!')
del company_list[0]
company_list.pop()
company_list.pop()
company_list.remove('Tencent')
print()
for j in company_list:
    print(f'{j}, thank you for passing my resume. I will attend the interview on time!')


运行


Hello Alibaba, here is my resume!
Hello Baidu, here is my resume!
Hello Tencent, here is my resume!
Hello MeiTuan, here is my resume!
Hello JD, here is my resume!
Baidu, thank you for passing my resume. I will attend the interview on time!


11.排序与反转(中等)


my_list = ['P', 'y', 't', 'h', 'o', 'n']
print('Here is the original list:')
print(my_list)
print()
print('The result of a temporary reverse order:')
print(sorted(my_list,reverse=True))
print()
print('Here is the original list again:')
print(my_list)
my_list.sort(reverse=True)
print()
print('The list was changed to:')
print(my_list)
my_list.reverse()
print()
print('The list was changed to:')
print(my_list)


运行


Here is the original list:
['P', 'y', 't', 'h', 'o', 'n']
The result of a temporary reverse order:
['y', 't', 'o', 'n', 'h', 'P']
Here is the original list again:
['P', 'y', 't', 'h', 'o', 'n']
The list was changed to:
['y', 't', 'o', 'n', 'h', 'P']
The list was changed to:
['P', 'h', 'n', 'o', 't', 'y']


12.数到20(入门)


使用一个 for 循环 或 while 循环 打印[1, 20]中的所有整数(一行一个数字)。


for i in range(1,21):
print(i)


运行


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20


13.朋友们的喜好(简单)


牛牛有一个name = ['Niumei', 'YOLO', 'Niu Ke Le', 'Mona'] 记录了他最好的朋友们的名字,请创建一个二维列表friends,使用append函数将name添加到friends的第一行。


假如Niumei最喜欢吃pizza,最喜欢数字3,YOLO最喜欢吃fish, 最喜欢数字6,Niu Ke Le最喜欢吃potato,最喜欢数字0,Mona最喜欢吃beef,最喜欢数字3。


请再次创建一个列表food依次记录朋友们最喜欢吃的食物,并将创建好的列表使用append函数添加到friends的第二行;


然后再创建一个列表number依次记录朋友们最喜欢的颜色,并将创建好的列表使用append函数添加到friends的第三行。


这样friends就是一个二维list,使用print函数直接打印这个二维list。


name = ['Niumei', 'YOLO', 'Niu Ke Le', 'Mona']
friends = []
friends.append(name)
food = ['pizza', 'fish', 'potato', 'beef']
friends.append(food)
number = [3, 6, 0, 3]
friends.append(number)
print(friends)


运行


[['Niumei', 'YOLO', 'Niu Ke Le', 'Mona'], ['pizza', 'fish', 'potato', 'beef'], [3, 6, 0, 3]]
相关文章
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
1454 7
|
开发者 Python
Python列表推导式:优雅与效率的完美结合
Python列表推导式:优雅与效率的完美结合
689 116
|
大数据 开发者 Python
Python列表推导式:简洁与高效的艺术
Python列表推导式:简洁与高效的艺术
581 109
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
725 3
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
928 3
|
存储 数据采集 监控
Python定时爬取新闻网站头条:从零到一的自动化实践
在信息爆炸时代,本文教你用Python定时爬取腾讯新闻头条,实现自动化监控。涵盖请求、解析、存储、去重、代理及异常通知,助你构建高效新闻采集系统,适用于金融、电商、媒体等场景。(238字)
1951 2
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
771 3
|
索引 Python
Python 列表切片赋值教程:掌握 “移花接木” 式列表修改技巧
本文通过生动的“嫁接”比喻,讲解Python列表切片赋值操作。切片可修改原列表内容,实现头部、尾部或中间元素替换,支持不等长赋值,灵活实现列表结构更新。
694 1
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
1138 0
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1982 102

推荐镜像

更多