Python 优雅编程技巧(一)

Python优雅编程技巧

1:交换赋值

  • 不推荐使用:

    temp = a
    a = b
    b = a
    
  • 推荐使用:

    a, b = b, a  
    生成一个元组(tuple)对象, 然后拆包
    

2:拆包

  • 不推荐使用:

    Fe_cow = ['Cu_cow', 'Ag_cow', 'Al_cow']
    first_name = Fe_cow[0]    # Cu_cow
    second_name = Fe_cow[1]  # Ag_cow
    last_name  = Fe_cow[2]   # Al_cow
    
  • 推荐使用:

    Fe_cow = ['Cu_cow', 'Ag_cow', 'Al_cow']
    first_name , second_name , last_name   = Fe_cow  
    

3:使用操作符in

  • 不推荐使用:

    # 多次判断
    if cow == 'Fe_cow' or cow == 'Ag_cow' or cow == 'Cu_cow':
    
  • 推荐使用:

    # 使用in 更加简洁
    if cow in ['Fe_cow', 'Ag_cow', 'Cu_cow']
    

4:字符串操作

  • 不推荐使用:

    ##不推荐
    colors = ['red', 'blue', 'green', 'yellow']
    result = ''
    for s in colors:
        result += s  #  每次赋值都丢弃以前的字符串对象, 生成一个新对象 
    
  • 推荐使用:

    colors = ['red', 'blue', 'green', 'yellow']
    result = ''.join(colors)  #  没有额外的内存分配
    

5:字典键值列表

  • 不推荐使用:

    colors = {'red': 33, 'blue': 44, 'green': 55, 'yellow': 66}
    for item in colors.keys():
        print(item)   # colors[item]
    
  • 推荐使用:

    colors = {'red': 33, 'blue': 44, 'green': 55, 'yellow': 66}
    for item in colors:
        print(item)
    
  • 只有当循环中需要更改key值的情况下,我们需要使用 my_dict.keys()

  • 生成静态的键值列表

6:字典键值判断

  • 不推荐使用:

    colors = {'red': 33, 'blue': 44, 'green': 55, 'yellow': 66}
    if colors.has_key('red'):
        print('存在健')
    
  • 推荐使用:

    colors = {'red': 33, 'blue': 44, 'green': 55, 'yellow': 66}
    if 'red' in colors:
        print('存在键')
    

7:判断真伪

  • 不推荐使用:

    if x == True:
    if len(x) != 0:
        # ...
    if x != []:
        # ... 
    
  • 推荐使用:

    if x:
        # ...
    

8:遍历列表以及索引

  • 不推荐使用:

    ##不推荐
    items = 'zero one two three'.split()
    # method 1
    i = 0
    for item in items:
        print i, item
        i += 1
    # method 2
    for i in range(len(items)):
        print i, items[i]
    
  • 推荐使用:

    ##推荐
    items = 'zero one two three'.split()
    for i, item in enumerate(items):
        print i, item
    

9:列表推导

  • 不推荐使用:

    a_list = ['zero', 'one', 'two', 'three']
    new_list = []
    for item in a_list:
        if len(a_list) == 4:
            new_list.append(item)
    print(new_list)
    
  • 推荐使用:

    a_list = ['zero', 'one', 'two', 'three']
    new_list = [ item for item in a_list if len(a_list) == 4 ]
    print(new_list)
    

10:列表推到嵌套:

  • 不推荐:

    a_list = ['zero', 'one', 'two', 'three', ['red', 'blue', 'green']]
    for b_list in a_list[4]:
        if len(b_list) == 3:
            for item in b_list:
                print(item)
    
  • 推荐使用:

    a_list = ['zero', 'one', 'two', 'three', ['red', 'blue', 'green']]
    c_list = [item for b_list in a_list for item in b_list if len(item) == 3]
    for i in c_list:
        print(i)
    

11:循环嵌套:

  • 不推荐使用:

    a_list = ['zero', 'one', 'two', 'three']
    b_list = ['red', 'blue', 'green']
    c_list = [0, 1, 2, 3]
    for a in a_list:
        print(a)
        for b in b_list:
            print(b)
            for c in c_list:
                print(c)
    
  • 推荐使用:

    from itertools import product
    for x, y, z in product(a_list, b_list, c_list):
        print(x)
        print(y)
        print(z)
    

你可能感兴趣的:(Python)