Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 3 column 1
---
- oeasy Python 0258
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
本教程同步发布在:
个人网站: `https://oeasy.org`
蓝桥云课: `https://www.lanqiao.cn/courses/3584`
GitHub: `https://github.com/overmind1980/oeasy-python-tutorial`
Gitee: `https://gitee.com/overmind1980/oeasypython`
---- 上次学习了两种函数的模式
- 重写(override/overwrite)
- 函数名相同、参数也相同
- 就是覆盖写
- 就是新的覆盖旧的
- 重载(overload)
- 函数名相同、参数不相同
- 通过引入相关的包
- 通过注释完成重载的工作
- 调用时会根据参数进行调用
- 我们回到最最简单的add函数
- 函数还有什么好玩的么?🤔
- 小型匿名函数可以用lambda这个关键字来
- 一般的函数定义
- lambda函数定义
- 这种函数可以直接写在参数位置
- 这样我们就有了一个lambda 关键字定义的匿名函数
- 如何从汇编角度理解这个?
- :python3 -m dis %
- 还是定义了一个函数
- 还是调用(call)了这个函数
- 只不过这个函数没有名字
- 而是叫做lambda
- 如何理解lambda呢?
- 查询
- lambda表达式经常用来产生匿名函数
- 用法和函数一样
- 有什么好用的么?
- 这个列表是由元组组成的
- 可以按照元组中的第0项排序
- 第0项是数字
- 按照数字的大小排序
- 也可以按照第1项
- 第1项是字符串
- 按照字符串中字母的ascii编码排序
- 默认是第几项呢?
- 默认第0项
- 可以把匿名函数变成非匿名的么?
- 可以定义成具体的函数
- 不过匿名函数(lambda)好写好用
- 具体应用
- 一副扑克
- 54张
- 想玩斗地主
- 用计算机模拟出洗牌、理牌的过程
from random import shuffle
def get_ordinal(l):
return l[0]
def show():
for group in groups:
for card in group:
print(card[1],end=" ")
print()
print()
red = "\33[30;47m"
black = "\33[31;47m"
reset = "\33[0m"
nums = ["2","3","4","5","6","7","8","9","X","J","Q","K","A"]
marks = [black + "\u2660",red + "\u2665",black + "\u2663",red + "\u2666"]
cards = []
current = 0
for num in nums:
for mark in marks:
cards.append((current,mark+num+reset))
current = current + 1
cards.append((53,black+"JK"+reset))
cards.append((54,red+"JK"+reset))
shuffle(cards)
user1 = cards[0:17]
user2 = cards[17:34]
user3 = cards[34:51]
dizhu = cards[51:54]
groups = [user1,user2,user3,dizhu]
show()
for group in groups:
group.sort(key=get_ordinal)
show()- 可以优化么?
- 如何优化
- 我们这次研究了匿名函数(lambda)
- 匿名函数也是一个函数
- 也需要被调用(call)
- 但是匿名函数用lambda表达式的方式写成
- 简便易用
- 清晰明确
- 可以用于排序等等方面
- 函数到这里已经很久了
- 是时候进行总结了🤔
- 下次再说👋
- 本文来自 oeasy Python 系统教程。
- 想完整、扎实学 Python,
- 搜索 oeasy 即可。














