美文网首页
Python Cookbook 笔记一

Python Cookbook 笔记一

作者: AIfred | 来源:发表于2018-04-26 22:19 被阅读0次

    前言

    • 笔记尽量按照书中内容进行顺序记录,但笔记中每块内容只是涉及自己所关心的内容展开,因此。这份笔记的内容可能会比较跳跃.
    • 每块小标题按照(###)来记录
    • 自己非科班出身,所以笔记中会夹渣这一些非常基础内容的解释
      *该书主要介绍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.

    相关文章

      网友评论

          本文标题:Python Cookbook 笔记一

          本文链接:https://www.haomeiwen.com/subject/uccvlftx.html