zoukankan      html  css  js  c++  java
  • Python cookbook

    Before:

    python built-in function: docs

    我只想学function map(), THIS  - 摘: map(foo, seq) is equivalent to [ foo(x) for x in seq]

                   - (看这个帖子,我好像明白了什么。好像第一次明白了,什么是“函数式编程”。)

    1.9 - 简化字符串的 translate 方法的使用

    WAIT- -!

    1.10 - 过滤字符串中不属于制定集合的字符

      1. 利用string.translate()处理中文,必须使用unicode。ASCII不能处理中文。

        这个问题花了我一个上午,哈哈,THIS

    #! /usr/bin/python
    #Filename: translate.py
    
    transTable = { ord('1'): u'', ord('2'): u''}
    unicodeStr = u'123'
    print unicodeStr.translate(transTable)

     1.13 - 访问子字符串

      1. Python struct

    struct.unpack_from(fmtbuffer[offset=0])

    Unpack the buffer according to the given format. The result is a tuple even if it contains exactly one item. The buffer must contain at least the amount of data required by the format (len(buffer[offset:]) must be at least calcsize(fmt)).

    struct.calcsize(fmt)

    Return the size of the struct (and hence of the string) corresponding to the given format.

    >>> import struct
    >>> baseformat = '5s 3x 8s 8s'
    >>> theline = '123456789012345678901234567890'
    >>> numremain = len(theline) - struct.calcsize(baseformat)
    >>> format = '%s %ds' % (baseformat, numremain)
    >>> format
    '5s 3x 8s 8s 6s'
    >>> l, s1, s2, t = struct.unpack(format, theline)
    >>> print l, s1, s2, t
    12345 90123456 78901234 567890

    1.18 - 一次完成多个替换 

    关键是 import re。 Python的 正则表达式re 怎么用? THIS 

    END

    ————更多交流,可以QQ:95二零7③9六
  • 相关阅读:
    Binary Tree Maximum Path Sum
    ZigZag Conversion
    Longest Common Prefix
    Reverse Linked List II
    Populating Next Right Pointers in Each Node
    Populating Next Right Pointers in Each Node II
    Rotate List
    Path Sum II
    [Leetcode]-- Gray Code
    Subsets II
  • 原文地址:https://www.cnblogs.com/kevin922/p/3160211.html
Copyright © 2011-2022 走看看