《流畅的python》这本确实老辣
最近在慢慢看几页,
第一章的示例代码,实现一副扑克牌。
确实老辣~
不是高手,没有这感觉,我慢慢学吧。
import collections
from random import choice
Card = collections.namedtuple('Card', ['rank', 'suit'])
class FrechDeck:
ranks = [str(n) for n in range(2, 11)] + list('JQKA')
suits = 'spades diamonds clubs hearts'.split()
def __init__(self):
self._cards = [Card(rank, suit) for suit in self.suits
for rank in self.ranks]
def __len__(self):
return len(self._cards)
def __getitem__(self, position):
return self._cards[position]
suit_values = dict(spades=3, hearts=2, diamonds=3, clubs=4)
def spades_high(card):
rank_value = FrechDeck.ranks.index(card.rank)
return rank_value * len(suit_values) + suit_values[card.suit]
beer_card = Card(', 'diamonds')
print(beer_card)
deck = FrechDeck()
print(len(deck))
print(deck[0])
print(deck[-1])
print(choice(deck))
print(deck[:3])
print(deck[12::13])
for card in deck:
# print(card)
pass
for card in reversed(deck):
# print(card)
pass
print(Card('Q', 'hearts') in deck)
', 'beats') in deck)
for card in sorted(deck, key=spades_high):
# print(card)
pass
输出:
Card(rank=', suit='diamonds') Card(rank=', suit='spades') Card(rank='A', suit='hearts') Card(rank=', suit='hearts') [Card(rank=', suit='spades')] [Card(rank='A', suit='spades'), Card(rank='A', suit='diamonds'), Card(rank='A', suit='clubs'), Card(rank='A', suit='hearts')] True False Process finished with exit code
《流畅的python》这本确实老辣的更多相关文章
- 流畅的python(笔记)
流畅的python中有很多奇技淫巧,整本书都在强调如何最大限度地利用Python 标准库.介绍了很多python的不常用的数据类型.操作.库等,对于入门python后想要提升对python的认识应该有 ...
- 流畅的python 对象引用 可变性和垃圾回收
对象引用.可变性和垃圾回收 变量不是盒子 人们经常使用“变量是盒子”这样的比喻,但是这有碍于理解面向对象语言中的引用式变量.Python 变量类似于 Java 中的引用式变量,因此最好把它们理解为附加 ...
- 《流畅的python》读书笔记
流畅的python 第1章 python数据模型 ---1.1 一摞Python风格的纸牌 特殊方法,即__method__,又被称为魔术方法(magic method)或者双下方法(dunder-m ...
- 《流畅的Python》一副扑克牌中的难点
1.现在在看<流畅的Python>这本书,看了三页就发现,这本书果然不是让新手来入门的,一些很常见的知识点能被这个作者玩出花来, 唉,我就在想,下面要分析的这些的代码,就算我费劲巴拉的看懂 ...
- [读书笔记]流畅的Python(Fluent Python)
<流畅的Python>这本书是图灵科技翻译出版的一本书,作者Luciano Ramalho. 作者从Python的特性角度出发,以Python的数据模型和特殊方法为主线,主要介绍了pyth ...
- 《流畅的Python》Object References, Mutability, and Recycling--第8章
Object References, Mutability, and Recycling 本章章节: Variables Are Not Boxes identity , Equality , Al ...
- 《流畅的Python》 第一部分 序章 【数据模型】
流畅的Python 致Marta,用我全心全意的爱 第一部分 序幕 第一章 Python数据模型 特殊方法 定义: Python解释器碰到特殊句法时,使用特殊方法激活对象的基本操作,例如python语 ...
- SyntaxError: Non-UTF-8 code starting with '\xbb' in file D:\流畅学python\ex32.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
1. 报错如下: SyntaxError: Non-UTF-8 code starting with '\xd3' in file D:\流畅学python\ex34.py on line 4, bu ...
- 流畅的Python读书笔记(二)
2.1 可变序列与不可变序列 可变序列 list. bytearray. array.array. collections.deque 和 memoryview. 不可变序列 tuple. str 和 ...
随机推荐
- 解题:国家集训队 Crash 的文明世界
题面 这种套着高次幂的统计问题一般都要用到第二类斯特林数和自然数幂的关系:$a^k=\sum\limits_{i=0}^{k}S_k^iC_a^i*i!$ 那么对于每个点$x$有: $ans_x=\s ...
- 最小化安装k8s
最小化安装k8s Nick_4438 关注 2018.07.11 10:40* 字数 670 阅读 0评论 0喜欢 0 1.前言 之前写过一篇二进制手工安装k8s的文章,过程复杂,搞了多日才安装成功. ...
- python---django中url路由分发
在urls.py文件中包含使用方法: from django.conf.urls import include, urlfrom django.contrib import admin urlpatt ...
- Git error: hint: Updates were rejected because the remote contains work that you do hint: not have locally
hint: Updates were rejected because the remote contains work that you dohint: not have locally. This ...
- 原始套接字-自定义IP首部和TCP首部
/* ===================================================================================== * * Filenam ...
- [转载]HTML5 真的会消灭原生App吗?
http://mp.weixin.qq.com/s?__biz=MzA5NjQ4MzkyMw==&mid=201728945&idx=1&sn=8f43b5bb10695efc ...
- Swagger文档化restful接口
1.注解 @Api:用在类上,说明该类的作用. @ApiOperation:注解来给API增加方法说明. @ApiImplicitParams : 用在方法上包含一组参数说明. @ApiImplici ...
- 在Windows 2008上安装Windows Mobile设备中心
我在windows2008系统上安装Microsoft Windows Mobile Device Center v6.1时,老是弹出对话框提示缺少一个Windows Mobile设备中心所需要的Wi ...
- Contrastive Loss (对比损失)
参考链接:https://blog.csdn.net/yanqianglifei/article/details/82885477 https://blog.csdn.net/qq_37053885/ ...
- Python 优雅获取本机 IP 方法
原文 见过很多获取服务器本地IP的代码,个人觉得都不是很好,例如以下这些 不推荐:靠猜测去获取本地IP方法 #!/usr/bin/env python # -*- coding: utf-8 -*- ...