python3—廖雪峰之练习(三)
2024-09-05 20:36:50
列表生成式练习
请修改列表生成式,通过添加if语句保证列表生成式能正确执行:
L1 = ['Hello', 'World', 18, 'Apple', None]
L2 = []
for x in L1:
if instance(x):
L2.append(x)
print(L2)
map/reduce练习
利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。
输入:['adam',‘LISA', 'barT'], 输出:['Adam','Lisa','Bart']:
def normalize(name):
return "%s" % (name[:1].upper() + name[1:])
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)
Python提供的sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list并利用reduce()求积:
from functools import reduce
def prod(L):
def product(x, y):
return x * y
return reduce(product, L)
print(prod(L))
利用map和reduce编写一个str2float函数,把字符串’123.456’转换成浮点数123.456:
str2float函数实现转自https://github.com/michaelliao/learnpython3/blob/master/samples/functional
from functools import reduce
CHAR_TO_FLOAT = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'.': -1
}
def str2float(s):
nums = map(lambda ch: CHAR_TO_FLOAT[ch], s)
point = 0
def to_float(f, n):
nonlocal point
if n == -1:
point = 1
return f
if point == 0:
return f * 10 + n
else:
point = point * 10
return f + n / point
return reduce(to_float, nums, 0.0)
print("str2float('\123.456\') = ", str2float('123.456'))
filter练习
回数是指从左向右读和从右向左读都是一样的数,例如12321,909。请利用filter()滤掉非回数:
def is_palidrome(n):
I, m = n, 0
while I:
m = m*10 + I%10
I = I // 10 #返I的整数部分,抛弃余数
return(n == m)
output = filter(is_palindrome, range(1, 1000))
print(output)
sort()函数练习
假设我们用一组tuple表示学生名字和成绩:
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
请用sorted()对上述列表分别按名字排序:
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
def by_name(t):
return t[0] #返回名字
L2 = sorted(L, key=by_name)
print(L2)
再按成绩从高到低排序:
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
def by_name(t):
return t[1] #返回成绩
L2 = sorted(L, key = by_name, reverse = True)
print(L2)
请编写一个decorator, 能在函数调用的前后打印出'begin call'和'end call'的日志:
转自http://blog.csdn.net/be_your_king/article/details/69938237
import functools
def log(func):
@functools.wraps(func) # wrap '__name__'
def wrapper(*args, **kw):
print("begin call [%s]" % (func.__name__))
func_tmp = func(*args, **kw)
print("end call [%s]" % (func.__name__))
return func_tmp
return wrapper
@log # 借助Python的@语法,把decorator置于函数的定义处
def hello():
print("hello")
hello()
最新文章
- 【bzoj1791】岛屿
- SQL Server 2008安装失败问题以及解决办法
- ARCI--做事情的重要方法论
- (转) eclipse debug (调试) 学习心得
- JSP标签库
- 搭建Go开发及调试环境(LiteIDE + GoClipse)
- EFI/GPT探索(为何win7分区时创建100M隐藏分区)
- VopSdk一个高逼格微信公众号开发SDK
- 微信JS-SDK开发 入门指南
- MySQL 报错 _DATA_TYPE_INVALID_
- 字符转码开源库libiconv目前还不支持64位
- SQL--server事物
- 用dbExpress页的SQLConnection1连接sql server2000怎么设置。 [问题点数:0分]
- spring boot 中添加mongodb支持
- ABP+AdminLTE+Bootstrap Table权限管理系统第十一节--Bootstrap Table用户管理列表以及Module Zero之用户管理
- js 捕获浏览器后退事件
- java 标准输入输出System.in与System.out
- Mysql 性能优化4 mysql参数配置
- ";Regressing Robust and Discriminative 3D Morphable Models with a very Deep Neural Network"; 解读
- [数学-构造矩阵]NEFU 1113
热门文章
- ubuntu 微信安装
- BZOJ 4897: [Thu Summer Camp2016]成绩单 动态规划
- Vue项目开发,nprogress进度条加载之超详细讲解及实战案例
- go语言系列--golang在windows上的安装和开发环境goland的配置
- jquery自动播放音频文件
- 使用resultMap定义查询结果集,实现关联查询
- 自建 CA 中心并签发 CA 证书
- 阶段3 1.Mybatis_07.Mybatis的连接池及事务_2 连接池介绍
- set_option()函数
- 移动端自动化==>;Windows-Android-Appium环境搭建