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六
  • 相关阅读:
    spring二级缓存的ehcache 的 配置文件
    C/C++联合(Union)浅谈
    C++技巧之名字空间namespace
    VC动态调用DLL的调试方法
    在C++中调用DLL中的函数
    VS编译debug模式静态库(lib)的结尾_d修改
    bash: chkconfig: command not found
    Linux在防火墙中开放SVN端口
    VIM选择文本块/复制/粘贴
    svnserve.conf:12: Option expected的问题解决方法[SVN]
  • 原文地址:https://www.cnblogs.com/kevin922/p/3160211.html
Copyright © 2011-2022 走看看