26个实用技巧,让你从此爱上python!

《26个实用技巧,让你从此爱上python!》 26个实用技巧,让你从此爱上python!

在日常工作中,我使用 python 完成数据科学的工作,在工作中,我总结了一些有用的技巧。

我尝试把这些技巧在字母a-z进行了分类。

下面是具体的小技巧

进群进群:943752371可以获取Python各类入门学习资料!

这是我的微信公众号【Python编程之家】各位大佬用空可以关注下,每天更新Python学习方法,感谢!

《26个实用技巧,让你从此爱上python!》 111111111111.png

all or any

python之所以如此流行的原因是,它的可读性非常高。而且有很多的语法糖,看下面的例子:

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>x = [True, True, False]
if any(x):
print(“At least one True”)
if all(x):
print(“Not one False”)
if any(x) and not all(x):
print(“At least one True and one False”)
</pre>

◆ ◆ ◆ ◆ ◆

bashplotlib

你想在控制台绘制图表?

pip install bashplotlib

bashplotlib是在控制台画图表的库。

◆ ◆ ◆ ◆ ◆

collections

python有很好的默认集合类,有时候我们需要一些增强的python类来完成对数据的处理,看下面列子:

(这个窗口可以左右移动哦!)

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>from collections import OrderedDict, Counter# Remembers the order the keys are added!
x = OrderedDict(a=1, b=2, c=3)# Counts the frequency of each character
y = Counter(“Hello World!”)
</pre>

◆ ◆ ◆ ◆ ◆

dir

你想知道python对象内部有什么东西吗?dir方法可以帮助你查看,看代码:

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>dir()
dir(“Hello World”)
dir(dir)
</pre>

这在你使用一个新的包时会非常有用。

《26个实用技巧,让你从此爱上python!》 26个实用技巧,让你从此爱上python!

emoji

python的emoji表情表:

$ pip install emoji

看代码:

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>from emoji import emojize
print(emojize(“:thumbs_up:”)
</pre>

◆ ◆ ◆ ◆ ◆

from future import

想提前尝试python的新特性,可以使用 future语法。

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>from future import print_function
print(“Hello World!”)
</pre>

◆ ◆ ◆ ◆ ◆

geopy

python的地理位置包

$ pip install geopy

使用这个包可以完成地址和经纬度的转换和距离检测等操作。看代码:

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>from geopy import GoogleV3place = “221b Baker Street, London”
location = GoogleV3().geocode(place)
print(location.address)
print(location.location)
</pre>

◆ ◆ ◆ ◆ ◆

howdoi

不知道一些事情该怎么做?用howdoi这个命令吧

$ pip install howdoi

看使用例子:

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>《26个实用技巧,让你从此爱上python!》 howdoi for loop in java $ howdoi undo commits in git
</pre>

《26个实用技巧,让你从此爱上python!》 26个实用技巧,让你从此爱上python!

inspect

想知道别人包的代码是是什么?使用inspect模块吧。看代码:

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import inspectprint(inspect.getsource(inspect.getsource)) print(inspect.getmodule(inspect.getmodule)) print
</pre>

◆ ◆ ◆ ◆ ◆

Jedi

python的代码补全器,如果没有用IDE编写python代码,可以尝试一下这个

◆ ◆ ◆ ◆ ◆

****kwargs**

python的参数语法糖,看代码:

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>dictionary = {“a”: 1, “b”: 2}def someFunction(a, b):
print(a + b) return# these do the same thing:
someFunction(**dictionary)someFunction(a=1, b=2)
</pre>

◆ ◆ ◆ ◆ ◆

List comprehensions

集合操作的语法糖,是不是很绕 哈哈

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>numbers = [1,2,3,4,5,6,7]
evens = [x for x in numbers if x % 2 is 0]
odds = [y for y in numbers if y not in evens]
cities = [‘London’, ‘Dublin’, ‘Oslo’]
def visit(city): print(“Welcome to “+city)for city in cities:
visit(city)
</pre>

《26个实用技巧,让你从此爱上python!》 26个实用技巧,让你从此爱上python!

map

函数式编程语法糖,不过现在很少用了

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>x = [1, 2, 3]
y = map(lambda x : x + 1 , x)# prints out [2,3,4]
print(list(y))
</pre>

◆ ◆ ◆ ◆ ◆

newspaper3k

用python读新闻,这个太极客了

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>$ pip install newspaper3k
</pre>

◆ ◆ ◆ ◆ ◆

Operator overloading

操作符重载是python的一个重要特性。在定义一个类时,可以用操作符重载来对两个对象做简单判断。看代码:

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>class Thing: def init(self, value):
self.__value = value
def gt(self, other):
return self.__value < other.__value
def lt(self, other):
return self.__value > other.__value
something = Thing(100)nothing = Thing(0)# True
something < nothing# False
something > nothing# Error
something + nothing
</pre>

◆ ◆ ◆ ◆ ◆

pprint

print方法可以做简单输出,如果要输出复杂对象,可以使用pprint

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import requestsimport pprint
url = ‘https://randomuser.me/api/?results=1′
users = requests.get(url).json()pprint.pprint(users)
</pre>

◆ ◆ ◆ ◆ ◆

Queue

python的队列实现

◆ ◆ ◆ ◆ ◆

repr

repr方法将一个对象用字符串表示。在定一个一个类时,可以重写repr方法

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>class someClass: def repr(self):
return “>some description here<“
someInstance = someClass()

prints >some description here<

print(someInstance)
</pre>

《26个实用技巧,让你从此爱上python!》 26个实用技巧,让你从此爱上python!

sh

用python执行shell命令

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>from sh import *sh.pwd()
sh.mkdir(‘new_folder’)
sh.touch(‘new_file.txt’)
sh.whoami()
sh.echo(‘This is great!’)
</pre>

◆ ◆ ◆ ◆ ◆

Type hints

python是一个弱类型语言,为了安全,还可以对方法参数声明类型。

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>def addTwo(x : Int) -< Int: return x + 2
</pre>

◆ ◆ ◆ ◆ ◆

uuid

生成uuid

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import uuiduser_id = uuid.uuid4()
print(user_id)
</pre>

◆ ◆ ◆ ◆ ◆

Virtual environments

为自己的项目虚拟一个独立的python环境,防止模块污染

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>python -m venv my-project
source my-project/bin/activate pip install all-the-modules
</pre>

《26个实用技巧,让你从此爱上python!》 26个实用技巧,让你从此爱上python!

wikipedia

python访问维基百科

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>import wikipediaresult = wikipedia.page(‘freeCodeCamp’)
print(result.summary)
for link in result.links: print(link)
</pre>

◆ ◆ ◆ ◆ ◆

xkcd

Humour is a key feature of the Python language — after all, it is named after the British comedy sketch show Monty Python’s Flying Circus. Much of Python’s official documentation references the show’s most famous sketches.

The sense of humour isn’t restricted to the docs, though. Have a go running the line below:

import antigravity

Never change, Python. Never change.

◆ ◆ ◆ ◆ ◆

YAML

ymal数据的python客户端

$ pip install pyyaml

import yaml

PyYAML lets you store Python objects of any datatype, and instances of any user-defined classes also.

◆ ◆ ◆ ◆ ◆

zip

将两个集合变为一个字典

<pre class=”ql-align-justify” style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>keys = [‘a’, ‘b’, ‘c’]
vals = [1, 2, 3]
zipped = dict(zip(keys, vals))
</pre>

《26个实用技巧,让你从此爱上python!》 26个实用技巧,让你从此爱上python!

python是一个非常灵活的语言。里面还有很多内置的语法糖,一方面方便了我们的编码,另外一方面也会造成一些困惑,有奇怪的语法记得多 谷歌。

    原文作者:浪里小白龙q
    原文地址: https://www.jianshu.com/p/f95051a3476f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞