当前位置: 代码迷 >> 综合 >> 廖雪峰python教程习题记录(自用)
  详细解决方案

廖雪峰python教程习题记录(自用)

热度:36   发布时间:2024-02-05 12:16:26.0

 高级特性

1.请使用迭代查找一个list中最小和最大值,并返回一个tuple:

# -*- coding: utf-8 -*-
def findMinAndMax(L):if L==[]:return (None, None)else:x=L[0]y=L[0]for i in L:if i>x:x=ielif i<y:y=ireturn (y, x)# 测试
if findMinAndMax([]) != (None, None):print('测试失败!')
elif findMinAndMax([7]) != (7, 7):print('测试失败!')
elif findMinAndMax([7, 1]) != (1, 7):print('测试失败!')
elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):print('测试失败!')
else:print('测试成功!')

 2.

# -*- coding: utf-8 -*-
L1 = ['Hello', 'World', 18, 'Apple', None]
L2 = [x.lower() for x in L1 if isinstance(x, str)]# 测试:
print(L2)
if L2 == ['hello', 'world', 'apple']:print('测试通过!')
else:print('测试失败!')

 

 

  相关解决方案