python 小技巧(python tricks) - 大数据技术参考_大数据技术文献_大数据趋势分析 -
当前位置:技术分享 > 技术参考 > 正文

python 小技巧(python tricks)2017-12-22 10:51:26 | 编辑:hely | 查看: | 评论:0

不积跬步无以至千里,人生苦短,我用Python,Life is short, use Python

不积跬步无以至千里

Github地址:https://github.com/vimiix/python_tricks

Slide链接:http://vimiix.com/python_tricks

人生苦短,我用Python

Life is short, use Python.

Python之禅

import this

'''
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
...
'''

多种方法来验证多条件语句

x, y, z = 0, 1, 0

if x == 1 or y == 1 or z == 1:
print('passed')

if 1 in (x, y, z):
print('passed')

# These only test for truthiness:
if x or y or z:
print('passed')

if any((x, y, z)):
print('passed')

函数参数解包

# Function argument unpacking

def myfunc(x, y, z):
print(x, y, z)

tuple_vec = (1, 0, 1)
dict_vec = {'x': 1, 'y': 0, 'z': 1}

myfunc(*tuple_vec)
# 1, 0, 1

myfunc(**dict_vec)
# 1, 0, 1

使用timeit模块来测试函数执行效率

import timeit

rt = timeit.timeit('"-".join(str(n) for n in range(100))',
number=10000)
print(rt)
# 0.252703905106

rt = timeit.timeit('"-".join([str(n) for n in range(100)])',
number=10000)
print(rt)
# 0.234980106354

rt = timeit.timeit('"-".join(map(str, range(100)))',
number=10000)
print(rt)
# 0.152318954468

合并两个字典在不同版本中的写法

x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}

# Python 3.5+
z = {**x, **y}
print(z)
# {'c': 4, 'a': 1, 'b': 3}

# Python 2.x
z = dict(x, **y)
print(z)
# {'a': 1, 'c': 4, 'b': 3}

巧用json模块更舒服的打印字典

# 标准打印出来,对于阅读不是很友好
my_mapping = {'a': 23, 'b': 42, 'c': 0xc0ffee}
print(my_mapping)
# {'b': 42, 'c': 12648430. 'a': 23} # 😞

# 使用json可以更好的展示
import json
print(json.dumps(my_mapping, indent=4, sort_keys=True))
# {
# "a": 23,
# "b": 42,
# "c": 12648430
# }

通过字典的值(value)排序输出

# How to sort a Python dict by value

xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}

print(sorted(xs.items(), key=lambda x: x[1]))
# [('d', 1), ('c', 2), ('b', 3), ('a', 4)]

# Or:

import operator
print(sorted(xs.items(), key=operator.itemgetter(1)))
# [('d', 1), ('c', 2), ('b', 3), ('a', 4)]

字典的get用法和默认值

name_for_userid = {
382: "Alice",
590: "Bob",
951: "Dilbert",
}

def greeting(userid):
return "Hi %s!" % name_for_userid.get(userid, "there")

print(greeting(382))
# "Hi Alice!"

print(greeting(2333333))
# "Hi there!"用namedtuple

来代替简单的类

from collections import namedtuple
Car = namedtuple('Car', 'color mileage')

# 实例化一个Car,并像类一样使用
my_car = Car('red', 3812.4)
print(my_car.color)
# 'red'
print(my_car.mileage)
# 3812.4

print(my_car)
# Car(color='red' , mileage=3812.4)

# 就像元组一样,namedtuple也不可更改:
my_car.color = 'blue'
# AttributeError: "can't set attribute"

上一篇:Spark RDD简介及RDD在Spark中的地位 Spark运行原理架构图(手绘)下一篇:

公众平台

搜索"raincent"或扫描下面的二维码