day5:函数练习题
2024-10-07 19:34:50
1、写函数,检查获取传入列表或者元祖的对象的所有奇数位索引的元素,并将作为新的列表返回给调用者
#解1:
def lis(x):
lis_1 = []
for i in range(len(x)):
#循环下标
if i % 2 == 1:
lis_1.append(x[i])
return lis_1
test = lis([5,32,54,66,4])
print(test) #解2:利用切片
def lis(x):
new_lis = x[1::2]#下标从1开始,【::】代表所有,2代表步长
return new_lis
print(lis([21,423,65,76,3,2]))
2、写函数,判断用户传入的对象、(字符串、列表、元祖)长度是否大于5.
def test(x):
if len(x) > 5:
return True
print(test([1,2,3]))
3、写函数,检查传入列表的长度,如果大于2,那么仅保留前2个长度的内容,并将新内容返回给调用者
def test(x):
if len(x) > 2 :
new_1 = x[0:2]
return new_1
print(test([2,3,5]))
4、写函数、计算传入字符串中的【数字】、【字母】、【空格】及【其他】的个数
def num(x):
num_1 = 0
str_1 = 0
spac = 0
other = 0
for i in x:
if i.isdecimal():
num_1 += 1
elif i.isspace() :
spac += 1
elif i.isalnum() :
str_1 += 1
else:
other += 1
return {'数字':num_1,'字母':str_1,'空格':spac,'其他':other}
test = num('fjakla3224/ .2 &^@')
print(test)
最新文章
- Polly
- cheap gucci bags for women finish fashion jewellery has to move
- 基于Maven site的穷人的本地知识管理系统
- codeforces 83 D. Numbers
- Oracle行转列操作
- OD使用教程3
- 9.SpringMVC和json结合传递数据 &;&; 10.SpringMVC获取controller中json的数据
- TCP/IP TIME_WAIT状态原理
- (转)Hessian(C#)介绍及使用说明
- 迅雷API:实现文件下载
- 如何通过fpmmm和zabbix来监控客户机上MariaDB数据库运行情况
- 水题 K
- linux Bash 常用
- python----函数与函数式编程
- 廖雪峰Java4反射与泛型-1反射-2访问字段Field和3调用方法Method
- socketserver模块实现并发和连接合法性验证
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Souvenirs 线段树套set
- 关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
- 每天一个linux命令(3):pwd
- (1)Oracle基础--用户与登录