Python Cookbook 笔记一

前言

  • 笔记尽量按照书中内容进行顺序记录,但笔记中每块内容只是涉及自己所关心的内容展开,因此。这份笔记的内容可能会比较跳跃.
  • 每块小标题按照(###)来记录
  • 自己非科班出身,所以笔记中会夹渣这一些非常基础内容的解释
    *该书主要介绍3.4,而我使用的是3.6程序,因此如果碰到有趣的3.6特性我也会一并写在这里。

书来源

切片器slice

切片器是解决硬编码的一个好方法
硬编码(Hard coding)

Hard coding (also hard-coding or hardcoding) is the software development practice of embedding an input or configuration data directly into the source code of a program or other executable object, or fixed formatting of the data, instead of obtaining that data from external sources or generating data or formatting in the program itself with the given input.

>>> items = [0, 1, 2, 3, 4, 5, 6]
>>> a = slice(2, 4)
>>> items[2:4]
[2, 3]
>>> items[a]
[2, 3]
>>> items[a] = [10,11]
>>> items
[0, 1, 10, 11, 4, 5, 6]
>>> del items[a]
>>> items
[0, 1, 4, 5, 6]

indices的使用

indices是slice类实例的一个方法,这个方法很好地解决了IndexError异常的问题
indices解释,来自官方帮助文档

indices(...) method of builtins.slice instance
S.indices(len) -> (start, stop, stride)

Assuming a sequence of length len, calculate the start and stop
indices, and the stride length of the extended slice described by
S. Out of bounds indices are clipped in a manner consistent with the
handling of normal slices.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 风中的草地 翩然而过孤独的紫色流星 一种生灵 由赴死的爱情羽化 天雨欲来 失侣的灵魂在绿中片刻静默 紫色的翅膀 使...
    忆荆高阅读 133评论 0 0
  • 枯枝李花飘落而下, 如赴一场美的盛夏。 花饰繁星, 点缀大地温暖的梦。 青石街道依昔, 追逐你留下的痕迹。 清风捎...
    爱笑的脚丫阅读 167评论 0 2
  • 很小的时候就听过一句话“百善孝为先”,孝是德的根本,很难想象一个对于自己的父母兄弟都不孝顺的人能有高尚的品德,治国...
    古今之变阅读 1,799评论 0 6
  • 通常来说,jQuery异步获取json数据有2种方式,一个是$.getJSON方法,一个是$.ajax方法。本文就...
    削铁如泥地瓜干阅读 432评论 0 1
  • 李某,女,39岁。 心慌,胸闷,难喘气。 微信问我:需不需要去医院检查。 告知:不用!先过来看! 察脉:脉气弦郁满...
    中医李奇飞阅读 226评论 0 1