出品 | AI科技大本营(ID:rgznai100) 【导读】2020年,你又立了什么新的 Flag?新一年,我们先为大家准备 30 个非常优秀的 Python 实践技巧。希望这些诀窍能在实际工作中帮助大家,并且学到一些有用的知识。由于官方从2020年1月1日起就停止了对python2.7的更新支持,因此本教程的大部分例子都只能在python 3环境下运行。如果你仍然在使用2.7版本,请先升级到python 3。你可以在代码中检查Python 版本,以确保你的代码使用者没有使用不兼容的版本运行脚本。使用以下代码进行简单的检查:if not sys.version_info > (2, 7): # berate your user for running a 10 year # python version elif not sys.version_info >= (3, 5): # Kindly tell your user (s)he needs to upgrade # because you're using 3.5 features
IPython 基本上是一个增强的shell,仅仅是为了自动补全功能,IPython也是值得使用的,但其实它还有更多作用,包括内建的Magic命令,这里列举了一些:%cd:改变当前的工作目录 %edit:打开编辑器并在关闭编辑器后执行键入的代码 %env:显示当前的环境变量 %pip:install [pkgs] 在不离开交互式shell的情况下安装功能包 %time 和 %timeit:类似于python中的time模块,可以为代码运行计时
完整的命令列表参见: https://ipython./en/stable/interactive/magics.html IPython的另一个有用功能是可以使用之前任意一行代码的输出,代码的输入和输入实际上都是对象,例如可以通过 Out[3] 来使用第三次运行代码的输出对象。列表解析式可以用来替换通过loop来填充列表的丑陋方法,其基本语法是:[ expression for item in list if conditional ]
mylist = [i for i in range(10)] print(mylist) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 由于可以使用表达式,因此可以通过更复杂的数学方法来生成列表:squares = [x**2 for x in range(10)] print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
def some_function(a): return (a + 5) / 2
my_formula = [some_function(i) for i in range(10)] print(my_formula) # [2, 3, 3, 4, 4, 5, 5, 6, 6, 7] 最后,也可以用if作为生成条件来对列表进行过滤。在下面的例子中,只有偶数被保留:filtered = [i for i in range(20) if i%2==0] print(filtered) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] 通过sys.getsizeof(object)命令可以查看任何对象的内存使用情况:import sys
mylist = range(0, 10000) print(sys.getsizeof(mylist)) # 48 这是因为range函数返回的是一个类对象,这个类对象表现为一个列表。因此使用range函数比使用实际的包含一万个数字的列表要更加节省内存。可以通过上面第四条中提到的列表解析式创建一个同样大小的实际列表:import sys
myreallist = [x for x in range(0, 10000)] print(sys.getsizeof(myreallist)) # 87632 实际内存占用87632字节,远高于通过range函数生成的对象。Pyhon中的函数都可以返回多个变量,而不需要字典,列表或者类作为返回对象。方法如下:def get_user(id): # fetch user from database # .... return name, birthdate
name, birthdate = get_user(4) 对于有限数量的返回值,这是可以的。但是任何超过3个值的内容都应该放到一个(data)类中。从3.7版本开始,python提供了 data 类。与常规类或其他替代方法(如返回多个值或字典)相比,有以下几个优点:from dataclasses import dataclass
@dataclass class Card: rank: str suit: str
card = Card("Q", "hearts")
print(card == card) # True
print(card.rank) # 'Q'
print(card) Card(rank='Q', suit='hearts') https:///python-data-classes/a = 1 b = 2 a, b = b, a print (a) # 2 print (b) # 1 从python 3.5开始,字典的合并变得更简单了:dict1 = { 'a': 1, 'b': 2 } dict2 = { 'b': 3, 'c': 4 } merged = { **dict1, **dict2 } print (merged) # {'a': 1, 'b': 3, 'c': 4} 在标题格式中,非介词的首字母会大写。可以通过.title()方法实现:mystring = "10 awesome python tricks" print(mystring.title()) '10 Awesome Python Tricks' 可以按任意字符来分割字符串,并存储到列表中,例如按空格来分割字符串:mystring = "The quick brown fox" mylist = mystring.split(' ') print(mylist) # ['The', 'quick', 'brown', 'fox'] 与上一条的功能正好相反,从列表中创建字符串,并在两个单词间插入空格:mylist = ['The', 'quick', 'brown', 'fox'] mystring = " ".join(mylist) print(mystring) # 'The quick brown fox' 也许你会疑惑,为什么不使用mylist.join(" ")呢?归根结底,String.join()函数不仅可以连接列表,还可以连接任何可迭代的列表。将它放在String中会阻止在多个位置实现相同的功能。这些表情具有很强的表达能力,能给人留下深刻印象。更重要的是,这在分析社交媒体数据时尤其有用。import emoji result = emoji.emojize('Python is :thumbs_up:') print(result) # 'Python is 👍'
# You can also reverse this: result = emoji.demojize('Python is 👍') print(result) # 'Python is :thumbs_up:' start,stop和step都是可选的(可填可不填),默认值是:# We can easily create a new list from # the first two elements of a list: first_two = [1, 2, 3, 4, 5][0:2] print(first_two) # [1, 2]
# And if we use a step value of 2, # we can skip over every second number # like this: steps = [1, 2, 3, 4, 5][0:5:2] print(steps) # [1, 3, 5]
# This works on strings too. In Python, # you can treat a string like a list of # letters: mystring = "abcdefdn nimt"[::2] print(mystring) # 'aced it' 可以用切片操作来翻转列表或字符串,将step设置为负值即可实现:revstring = "abcdefg"[::-1] print(revstring) # 'gfedcba'
revarray = [1, 2, 3, 4, 5][::-1] print(revarray) # [5, 4, 3, 2, 1] 可以通过Pillow模块来显示图片,例如显示Kitty小猫,首先安装python图片库:然后下载你要显示的图片,并重命名。然后可以通过以下命令来显示图片:from PIL import Image
im = Image.open("kittens.jpg") im.show() print(im.format, im.size, im.mode) # JPEG (1920, 1357) RGB Pillow的功能远不止显示图片。它可以对图片进行分析,调整大小,滤波,增强,变形等等。更多资料详见文档:https://pillow./en/stable/。Python的一个内建函数是 map()。map()的基本语法是:map(function, something_iterable) 传入的参数是一个函数,和一个需要执行的对象,可以是任何可迭代对象。在下面的例子中使用的是list:def upper(s): return s.upper()
mylist = list(map(upper, ['sentence', 'fragment'])) print(mylist) # ['SENTENCE', 'FRAGMENT']
# Convert a string representation of # a number into a list of ints. list_of_ints = list(map(int, "1234567"))) print(list_of_ints) # [1, 2, 3, 4, 5, 6, 7] map()是一个代替循环的好方式,可以在你的代码中尝试使用map()函数。通过set()函数可以将列表或字符串转换为集合,集合中的不含重复元素:mylist = [1, 1, 2, 3, 4, 5, 5, 5, 6, 6] print (set(mylist)) # {1, 2, 3, 4, 5, 6}
# And since a string can be treated like a # list of letters, you can also get the # unique letters from a string this way: print (set("aaabbbcccdddeeefff")) # {'a', 'b', 'c', 'd', 'e', 'f'}
test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4] print(max(set(test), key = test.count)) # 4 因此在上面的这行语句中我们首先找出了test列表的所有独特值,即{1,2,3,4}。接着,将.count函数应用于set中的每个值,得到一个数量列表,然后通过max找出数量最大的值。可以自行创建进度条,但也可以通过progress模块来快速创建:from progress.bar import Bar
bar = Bar('Processing', max=20) for i in range(20): # Do some work bar.next() bar.finish() 21、在交互式shell中使用用 _ 来获取上一个表达式的运行结果可以使用下划线操作符来获取最后运行的表达式的输出,在IPython中操作如下:In [1]: 3 * 3 Out[1]: 9 In [2]: _ + 3 Out[2]: 12 这种方法在python shell中也是适用的,IPython还可以通过Out[n]来获取任意第n个表达式的输出结果。您可以快速启动web服务器,来提供当前工作目录的内容:如果您想与同事共享一些内容,或者想测试一个简单的HTML站点,这是非常有用的。23、多行字符串 虽然可以使用三重引号在代码中包含多行字符串,但这并不理想。在三重引号之间的所有内容都变成字符串,包括格式。相比我更喜欢第二种方式,它将多行连接在一起,允许你很好地格式化代码。惟一的缺点是需要显式地放入新行:s1 = """Multi line strings can be put between triple quotes. It's not ideal when formatting your code though""" print (s1) # Multi line strings can be put # between triple quotes. It's not ideal # when formatting your code though s2 = ("You can also concatenate multiple\n" + "strings this way, but you'll have to\n" "explicitly put in the newlines") print(s2) # You can also concatenate multiple # strings this way, but you'll have to # explicitly put in the newlines 这是另一种使你代码变得简洁,同时保持可读性的方法:[on_true] if [expression] else [on_false]
x = "Success!" if (y == 2) else "Failed!" 可以使用Collections依赖包中的Counter方法来获得一个包含列表中所有惟一元素计数的字典:from collections import Counter
mylist = [1, 1, 2, 3, 4, 5, 5, 5, 6, 6] c = Counter(mylist) print(c) # Counter({1: 2, 2: 1, 3: 1, 4: 1, 5: 3, 6: 2})
# And it works on strings too: print(Counter("aaaaabbbbbccccc")) # Counter({'a': 5, 'b': 5, 'c': 5}) 可以在python中链接比较运算符,从而使代码更简洁,可读性更强:x = 10 # Instead of: if x > 5 and x < 15: print("Yes") # yes # You can also write: if 5 < x < 15: print("Yes") # Yes 通过 Colorama 依赖包,可以在终端中添加更多色彩:from colorama import Fore, Back, Style print(Fore.RED + 'some red text') print(Back.GREEN + 'and with a green background') print(Style.DIM + 'and in dim text') print(Style.RESET_ALL) print('back to normal now') https:///project/colorama/python-dateutil模块为标准的datetime模块提供了强大的扩展。首先安装该模块:pip3 install python-dateutil 你可以用这个库做很多很酷的事情。我讲把我认为特别有用的一个功能作为示例:日志文件中日期的模糊解析等。如下:from dateutil.parser import parse logline = 'INFO 2020-01-01T00:00:01 Happy new year, human.' timestamp = parse(log_line, fuzzy=True) print(timestamp) # 2020-01-01 00:00:01 只要记住,如果datatime不具备某个功能,那datautil一定有该功能,datautil是datatime功能的延续。在Python 2中,除法运算符(/)默认为整数除法,除非其中一个操作数是浮点数。如下:# Python 2 5 / 2 = 2 5 / 2.0 = 2.5 在python3中,除法运算符/默认为浮点除法,//运算符变成了整数除法。所以有:Python 3 5 / 2 = 2.5 5 // 2 = 2 对于这种变化背后的完整动机,可以阅读PEP-0238:https://www./dev/peps/pep-0238/可以使用chardet模块来检测文件中的字符集合。这在分析大量随机文本时非常有用。安装chardet模块:现在你有了一个额外的命令行工具chardetect,它可以这样使用:chardetect somefile.txt somefile.txt: ascii with confidence 1.0 你也可以通过编程的方式来使用这个依赖包,详见技术文档:https://chardet./en/latest/usage.html以上就是30个python的技巧,希望这些技巧能帮助你在新的一年里有个不错的开始。
|