21

经验丰富程序员才知道的8种高级Python技巧

 3 years ago
source link: http://developer.51cto.com/art/202007/622016.htm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

本文转载自公众号“读芯术”(ID:AI_Discovery)

本文将介绍8个简洁的Python技巧,若非经验十足的程序员,你肯定有些从未见过。向着更简洁更高效,出发吧!

nUfUJ3J.jpg!web

1.通过多个键值将对象进行排序

假设要对以下字典列表进行排序:

people = [ 
{ 'name': 'John', "age": 64 }, 
{ 'name': 'Janet', "age": 34 }, 
{ 'name': 'Ed', "age": 24 }, 
{ 'name': 'Sara', "age": 64 }, 
{ 'name': 'John', "age": 32 }, 
{ 'name': 'Jane', "age": 34 }, 
{ 'name': 'John', "age": 99 }, 
] 

不仅要按名字或年龄对其进行排序,还要将两个字段同时进行排序。在SQL中,会是这样的查询:

SELECT * FROM people ORDER by name, age 

实际上,这个问题的解决方法可以非常简单,Python保证sort函数提供了稳定的排序顺序,这也意味着比较相似的项将保留其原始顺序。要实现按名字和年龄排序,可以这样做:

import operator 
people.sort(key=operator.itemgetter('age')) 
people.sort(key=operator.itemgetter('name')) 

要注意如何反转顺序。首先按年龄分类,然后按名字分类,使用operator.itemgetter()从列表中的每个字典中获取年龄和名字字段,这样你就会得到想要的结果:

[ 
{'name': 'Ed', 'age': 24}, 
{'name': 'Jane', 'age': 34}, 
{'name': 'Janet','age': 34}, 
{'name': 'John', 'age': 32}, 
{'name': 'John', 'age': 64}, 
{'name': 'John', 'age': 99}, 
{'name': 'Sara', 'age': 64} 
] 

名字是主要排序项,如果姓名相同,则以年龄排序。因此,所有John都按年龄分组在一起。

2.数据类别

自3.7版之后,Python开始能提供数据类别。比起常规类或其他替代方法(如返回多个值或字典),它有着更多优点:

  • 数据类需要很少的代码
  • 可以比较数据类,因为 __eq__ 可以实现此功能
  • 数据类需要类型提示,减少了发生错误的可能性
  • 可以轻松打印数据类以进行调试,因为__repr__可以实现此功能

这是一个工作中的数据类示例:

from dataclasses import dataclass 
                  @dataclass 
                 classCard: 
                     rank: str 
                     suit: str 
                       card=Card("Q", "hearts") 
                  print(card == card) 
                 # True 
                  print(card.rank) 
                 # 'Q' 
                  print(card) 
                 Card(rank='Q', suit='hearts') 

3.列表推导

列表推导可以在列表填写里代替讨厌的循环,其基本语法为

[ expression for item in list if conditional ] 

来看一个非常基本的示例,用数字序列填充列表:

mylist = [i for i inrange(10)] 
                print(mylist) 
                # [0, 1, 2, 3,4, 5, 6, 7, 8, 9] 

因为可以使用表达式,所以你还可以进行一些数学运算:

squares = [x**2for x inrange(10)] 
                print(squares) 
                # [0, 1, 4, 9,16, 25, 36, 49, 64, 81] 

甚至能调用外部函数:

defsome_function(a): 
                                return (a +5) /2 
                                
                            my_formula= [some_function(i) for i inrange(10)] 
                            print(my_formula) 
                            # [2.5, 3.0,3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0] 

最后,可以使用if函数来筛选列表。在这种情况下,只保留可被2除的值:

filtered = [i for i inrange(20) if i%2==0] 
               print(filtered) 
               # [0, 2, 4, 6,8, 10, 12, 14, 16, 18] 

4.检查对象的内存使用情况

使用sys.getsizeof()可以检查对象的内存使用情况:

import sys 
                  mylist =range(0, 10000) 
          print(sys.getsizeof(mylist)) 
          # 48 

为什么这个庞大的列表只有48个字节?这是因为range函数返回的类表现为列表。与使用实际的数字列表相比,数序列的存储效率要高得多。我们可以通过列表推导来创建相同范围内的实际数字列表:

import sys 
                  myreallist = [x for x inrange(0, 10000)] 
          print(sys.getsizeof(myreallist)) 
          # 87632 

通过使用sys.getsizeof(),我们可以了解更多关于Python和内存使用情况的信息。

5.查找最频繁出现的值

要查找列表或字符串中最频繁出现的值:

test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4] 
     print(max(set(test), key = test.count)) 
     # 4 
  • max()将返回列表中的最大值。key参数采用单个参数函数自定义排序顺序,在本例中为test.count,该函数适用于迭代器上的每个项目。
  • test.count是list的内置功能。它接受一个参数,并计算该参数的出现次数。因此test.count(1)将返回2,而test.count(4)将返回4。
  • set(test)返回test中的所有唯一值,所以{1、2、3、4}

那么在这一行代码将接受test的所有唯一值,即{1、2、3、4}。接下来,max将对其应用list.count 函数并返回最大值。

还有一种更有效的方法:

from collections import Counter 
Counter(test).most_common(1) 
# [4: 4] 

6.属性包

你可以使用attrs代替数据类,选择attrs有两个原因:

  • 使用的Python版本高于3.7
  • 想要更多功能

Theattrs软件包支持所有主流Python版本,包括CPython 2.7和PyPy。一些attrs可以提供验证器和转换器这种超常规数据类。来看一些示例代码:

@attrs 
          classPerson(object): 
              name =attrib(default='John') 
              surname =attrib(default='Doe') 
              age =attrib(init=False) 
              p =Person() 
          print(p) 
          p=Person('Bill', 'Gates') 
          p.age=60 
          print(p) 
                  # Output: 
          #   Person(name='John', surname='Doe',age=NOTHING) 
          #   Person(name='Bill', surname='Gates', age=60) 

实际上,attrs的作者已经在使用引入数据类的PEP了。数据类被有意地保持得更简单、更容易理解,而attrs 提供了可能需要的所有特性。

7.合并字典(Python3.5+)

dict1 = { 'a': 1, 'b': 2 } 
        dict2= { 'b': 3, 'c': 4 } 
        merged= { **dict1, **dict2 } 
        print (merged) 
        # {'a': 1, 'b':3, 'c': 4} 

如果有重叠的键,第一个字典中的键将被覆盖。在Python 3.9中,合并字典变得更加简洁。上面Python 3.9中的合并可以重写为:

merged = dict1 | dict2 

8.返回多个值

Python中的函数在没有字典,列表和类的情况下可以返回多个变量,它的工作方式如下:

defget_user(id): 
                       # fetch user from database 
                       # .... 
                       return name, birthdate 
                  name, birthdate =get_user(4) 

这是有限的返回值,但任何超过3个值的内容都应放入一个(数据)类。

这8个小技巧足够你好好消化一阵儿啦!

6Fveeij.jpg!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK