Python入门:04 列表(上)NP16 - NP21
NP16 发送offer
描述
某公司在面试结束后,创建了一个依次包含字符串 'Allen'
和 'Tom'
的列表offer_list,
作为通过面试的名单。
请你依次对列表中的名字发送类似 'Allen, you have passed our interview and will soon become a member of our company.'
的句子。
但由于Tom
有了其他的选择,没有确认这个offer
,HR选择了正好能够确认这个offer
的And
y,所以请把列表offer_list
中 'Tom'
的名字换成 'Andy'
,再依次发送类似 'Andy, 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!
如果没有思路可以到题解查看
代码:
offer_list = ['Allen', 'Tom']
for i in range(len(offer_list)):
print('{}, you have passed our interview and will soon become a member of our company.'.format(offer_list[i]))
for str_i in offer_list:
if str_i == 'Tom':
print('Andy, welcome to join us!' )
else:
print('{}, welcome to join us!'.format(str_i) )
保存提交:
分享解题思路
输入代码、确定
发送:
NP17 生成列表
描述
一串连续的数据用什么记录最合适,牛牛认为在Python中非列表(list)莫属了。现输入牛牛朋友们的名字,请使用list
函数与split
函数将它们封装成列表,再整个输出列表。
输入描述:
输入一串字符串,它们之间通过空格间隔开。
输出描述:
输出封装后的列表。
示例1
输入:NiuNiu NiuMei NiuNeng
输出:['NiuNiu', 'NiuMei', 'NiuNeng']
代码:
print(input().split(" "))
自测通过后保存提交
NP18 生成数字列表
描述
牛牛在牛客网系统录入了一连串数字,数字之间依靠逗号隔开,你能帮助他将这些数字存储在列表中吗,列表元素以int
的形式。
输入描述:
输入一行整数,数字之间以空格间隔。
输出描述:
输出这些数字组成的完整列表。
示例1
输入:1 2 3 4 5
输出:[1, 2, 3, 4, 5]
代码:
print([int(i) for i in input().split(" ")])
自测通过后保存提交
NP19 列表的长度
描述
牛牛学会了使用list
函数与split
函数将输入的连续字符串封装成列表,你能够帮他使用len
函数统计一些公输入了多少字符串,列表中有多少元素吗?
输入描述:
输入一行多个字符串,字符串之间通过空格间隔。
输出描述:
输出列表的长度。
示例1
输入:NiuNiu NiuMei NiuNeng
输出:3
代码:
print(len(input().split(" ")))
自测通过后保存提交
NP20 增加派对名单(一)
描述
为庆祝驼瑞驰在牛爱网找到合适的对象,驼瑞驰通过输入的多个连续字符串创建了一个列表作为派对邀请名单,在检查的时候发现少了他最好的朋友“Allen”
的名字,你能使用append
函数将这个名字加到列表末尾吗?添加完成请输出完整列表。
输入描述:
输入多个连续的字符串表示名字,以空格间隔。
输出描述:
输出添加完Allen
后的完整列表。
示例1
输入:Niuniu Niumei Lucy Niuneng
输出:['Niuniu', 'Niumei', 'Lucy', 'Niuneng', 'Allen']
代码:
lst = input().split(" ")
lst.append('Allen')
print(lst)
自测通过后保存提交
NP21 增加派对名单(二)
描述
为庆祝驼瑞驰在牛爱网找到合适的对象,驼瑞驰通过输入的多个连续字符串创建了一个列表作为派对邀请名单,在检查的时候发现少了他最好的朋友“Allen”
的名字,因为是最好的朋友,他想让这个名字出现在邀请列表的最前面,你能用insert
函数帮他实现吗?请输出插入后的完整列表。
输入描述:
输入多个连续的字符串表示名字,用空格间隔。
输出描述:
输出插入名字后的完整列表。
示例1
输入:Niuniu Niumei Lucy
输出:['Allen', 'Niuniu', 'Niumei', 'Lucy']
代码:
lst = input().split(" ")
lst.insert(0, 'Allen')
print(lst)
自测:
提交: