牛客题霸在线编程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]]
相关文章
|
1天前
|
开发者 Python
Python入门:8.Python中的函数
### 引言 在编写程序时,函数是一种强大的工具。它们可以将代码逻辑模块化,减少重复代码的编写,并提高程序的可读性和可维护性。无论是初学者还是资深开发者,深入理解函数的使用和设计都是编写高质量代码的基础。本文将从基础概念开始,逐步讲解 Python 中的函数及其高级特性。
Python入门:8.Python中的函数
|
1天前
|
存储 索引 Python
Python入门:6.深入解析Python中的序列
在 Python 中,**序列**是一种有序的数据结构,广泛应用于数据存储、操作和处理。序列的一个显著特点是支持通过**索引**访问数据。常见的序列类型包括字符串(`str`)、列表(`list`)和元组(`tuple`)。这些序列各有特点,既可以存储简单的字符,也可以存储复杂的对象。 为了帮助初学者掌握 Python 中的序列操作,本文将围绕**字符串**、**列表**和**元组**这三种序列类型,详细介绍其定义、常用方法和具体示例。
Python入门:6.深入解析Python中的序列
|
1天前
|
程序员 UED Python
Python入门:3.Python的输入和输出格式化
在 Python 编程中,输入与输出是程序与用户交互的核心部分。而输出格式化更是对程序表达能力的极大增强,可以让结果以清晰、美观且易读的方式呈现给用户。本文将深入探讨 Python 的输入与输出操作,特别是如何使用格式化方法来提升代码质量和可读性。
Python入门:3.Python的输入和输出格式化
|
1天前
|
缓存 算法 数据处理
Python入门:9.递归函数和高阶函数
在 Python 编程中,函数是核心组成部分之一。递归函数和高阶函数是 Python 中两个非常重要的特性。递归函数帮助我们以更直观的方式处理重复性问题,而高阶函数通过函数作为参数或返回值,为代码增添了极大的灵活性和优雅性。无论是实现复杂的算法还是处理数据流,这些工具都在开发者的工具箱中扮演着重要角色。本文将从概念入手,逐步带你掌握递归函数、匿名函数(lambda)以及高阶函数的核心要领和应用技巧。
Python入门:9.递归函数和高阶函数
|
1天前
|
存储 SQL 索引
Python入门:7.Pythond的内置容器
Python 提供了强大的内置容器(container)类型,用于存储和操作数据。容器是 Python 数据结构的核心部分,理解它们对于写出高效、可读的代码至关重要。在这篇博客中,我们将详细介绍 Python 的五种主要内置容器:字符串(str)、列表(list)、元组(tuple)、字典(dict)和集合(set)。
Python入门:7.Pythond的内置容器
|
1天前
|
存储 Linux iOS开发
Python入门:2.注释与变量的全面解析
在学习Python编程的过程中,注释和变量是必须掌握的两个基础概念。注释帮助我们理解代码的意图,而变量则是用于存储和操作数据的核心工具。熟练掌握这两者,不仅能提高代码的可读性和维护性,还能为后续学习复杂编程概念打下坚实的基础。
Python入门:2.注释与变量的全面解析
|
1天前
|
知识图谱 Python
Python入门:4.Python中的运算符
Python是一间强大而且便捷的编程语言,支持多种类型的运算符。在Python中,运算符被分为算术运算符、赋值运算符、复合赋值运算符、比较运算符和逻辑运算符等。本文将从基础到进阶进行分析,并通过一个综合案例展示其实际应用。
|
4月前
|
存储 安全 Serverless
Python学习四:流程控制语句(if-else、while、for),高级数据类型(字符串、列表、元组、字典)的操作
这篇文章主要介绍了Python中的流程控制语句(包括if-else、while、for循环)和高级数据类型(字符串、列表、元组、字典)的操作。
68 0
|
4月前
|
存储 JSON 数据处理
分析、总结Python使用列表、元组、字典的场景
分析、总结Python使用列表、元组、字典的场景
57 0
|
4月前
|
存储 自然语言处理 Java
【Python】列表和元组
【Python】列表和元组
35 0

热门文章

最新文章

推荐镜像

更多