Python Tips

Just some python tips by lzhbrian

Basic

Basic - 基本运算

  1. python 除法

    # python2
    10 / 3 = 3
    10 / 3.0 = 3.333333
    10 // 3 = 3
    10 // 3.0 = 3.0 # 截取小数部分但还是小数
    
    # python3
    10 / 3 = 3.333333
    10 / 3.0 = 3.333333
    10 // 3 = 3
    10 // 3.0 = 3.0 # 截取小数部分但还是小数
    
  2. 查看变量类型

    type()
    
  3. ord, chr, unichr

    chr(65) = 'A'
    ord('a') = 97
    unichr(12345) = u'\u3039'
    

Bit manipulation

Bit manipulation - 进制转换

  1. 10进制转换成2、8、16进制

    bin();oct();hex()
    60 = 0b11100 = 0o74 = 0x3c
    
  2. 其他进制转换成10进制

    int('101',2) = 5
    int('17',8) = 15
    

Bit manipulation - 位操作

  • 二进制操作
    • &:按位与
    • |:按位或
    • ^:按位异或
    • ~:取反
    • <<:左移
    • >>: 右移

List

List - Sort

  • Two method

    a.sort() # did on a
    a = sorted(a, reverse = True) # gen a new item
    

List - Counter

  1. count 函数

    a = [1,1,1,'1']
    a.count(1) = 3
    a.count('1') = 1
    
  2. collections.Counter 模块 计数

    • http://www.zlovezl.cn/articles/collections-in-python/

      from collections import Counter
      s = '''A Counter i....'''.lower()
      
      c = Counter(s)
      # 获取出现频率最高的5个字符
      print c.most_common(5)
      
      # Result:
      [(' ', 54), ('e', 32), ('s', 25), ('a', 24), ('t', 24)]
      

List - Join, Split

  1. http://wangwei007.blog.51cto.com/68019/1100587

    li = ['my','name','is','bob']
    '_'.join(li) = 'my_name_is_bob'
    'a  b'.split(' ') = ['a','','b'] # 中间两个空格
    

List - Extended Slices

  1. Official explanation

    https://docs.python.org/2/whatsnew/2.3.html#extended-slices

    L = range(11) # L = [0,1,2,3,4,5,6,7,8,9,10]
    L[::2] # = [0,2,4,6,8,10]
    L[0:10:2] # = [0,2,4,6,8]
    L[::-1] # L = [10,9,8,7,6,5,4,3,2,1,0]
    

List - Special Usage

  1. 获取特定index范围

    # list
    a = [1,2,3,4,5,6]
    a[:3] = [1,2,3]
    a[3:] = [4,5,6]
    
  2. 获取数组最后一个元素 Last element

    a = [1,2,3,4]
    a[-1] = 4
    
  3. 反转 Reverse

    a = [1,2,3]
    a[::-1] = [3,2,1]
    
  4. range 用法

    range(10) == range(0,10) == range(0,10,1) == [0,1,2,3,4,5,6,7,8,9]
    range(10,0,-1) == [10,9,8,7,6,5,4,3,2,1] == range(1,11)[::-1]
    
  5. Copy

    a = [1,2,3,4,5]
    b = a
    b.append(6)
    print a # [1,2,3,4,5,6]
    
    b = a[:]
    b.append(6)
    print a # [1,2,3,4,5]
    

List - Bisect

  1. 插入:

    import bisect as bi
    
    # insert to the left
    bi.insort_left(l, val) # bi.insort_left(l, val, lo=0, hi=len(l))
    l.insert(bi.bisect_left(l, val), val) # same as above
    
    # Below is equivalent, insert to the right
    bi.insort_right(l, val)
    bi.insort(l, val)
    
  2. 查找插入的位置

    import bisect as bi
    
    # return the index of 'will insert place' (left)
    bi.bisect_left(l, val) # bi.bisect_left(l, val, lo=0, hi=len(l))
    
    # Below is equivalent, return the index of 'will insert place' (right)
    bi.bisect_right(l, val)
    bi.bisect(l, val)
    

Dict

Dict - 遍历 Traverse

  • 遍历:

    for k,v in d.items():
      print k,v
    

Dict - 排序 Sort

  1. Using operator

    # Sort by keys
    import operator
    x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
    sorted_x = sorted(x.items(), key=operator.itemgetter(0),reverse=True)
    for k,v in sorted_x: # Return a tuple, can directly use: 'for k,v in sorted_x'
        ...
    
    # Sort by values:
    import operator
    x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
    sorted_x = sorted(x.items(), key=operator.itemgetter(1),reverse=True)
    
  2. Using lambda

    # Sort by keys:
    sorted(d.items(), key=lambda x: x[0],reverse=True)
    
    # Sort by values:
    sorted(d.items(), key=lambda x: x[1],reverse=True)
    

Set

Set - Basic Usage

  1. Basic

    x.add('d') # 添加一项 
    x.update([10,37,42]) # 在s中添加多项
    x.remove('H')
    x in s # 测试 x 是否是 s 的成员
    x not in s # 测试 x 是否不是 s 的成员
    
  2. symbol

    x & y # 交集
    x | y # 并集
    x - y # 差集
    x ^ y # 对称差集(项在x或sy中,但不会同时出现在二者中)
    
  3. Others

    # 测试是否 s 中的每一个元素都在 t 中
    s.issubset(t) == True
    s <= t
    
    # 测试是否 t 中的每一个元素都在 s 中
    s.issuperset(t)
    s >= t
    
    # 删除 set s 中的所有元素
    s.clear()
    

    ​

    ​

File

File - Read csv

# First Row is column name
with open(path, 'r') as f:
    reader = csv.DictReader(f)
    # fields: No., Time, Source, Destination, Protocol, Length, Host, Info, Request URI Query Parameter
    for row in reader:
        print row["Info"]
# First Row is data, need to specify column name
with open('names.csv', 'w') as csvfile:
    fieldnames = ['first_name', 'last_name']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
# Does not use column name, row is a list instead
with open('some.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print row

Special

Special - with

with open("x.txt") as f:
    data = f.read()
    # do something with data
with open("x.txt") as f1, open('xxx.txt') as f2:
    # do something with f1,f2

Special - Levenshtein Distance

  1. Installation:

    Ref: https://github.com/ztane/python-Levenshtein/

    Ref: http://www.cnblogs.com/kaituorensheng/archive/2013/05/18/3085653.html

    pip install python-Levenshtein
    
  2. Usage:

    import Levenshtein
    
    # Calc Levenshtein dDistance (or Edit Distance)
    # 删除、插入、替换 +1
    Levenshtein.distance(str1, str2)
    
    # Calc Levenshtein Ratio, r = (sum - ldist) / sum
    # sum = len(str1) + len(str2)
    # but 删除、插入 +1,替换 +2
    Levenshtein.ratio(str1, str2)
    
    # Calc Hamming Distance
    # len must be the same
    Levenshtein.hamming(str1, str2)
    
    # Calc Jaro Distance
    Levenshtein.jaro(s1, s2)
    
    # Calc Jaro–Winkler Distance
    Levenshtein.jaro_winkler(s1, s2)
    

    ​