程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Advanced Series (8)

編輯:Python

Catalog

Containers (Collections)

defaultdict

Counter

deque

namedtuple

enum.Enum (Python 3.4+)


Containers (Collections)

Python Comes with a module , It contains many container data types , It's called collections. We will discuss its function and usage .

What we are going to discuss is :

  1. defaultdict
  2. counter
  3. deque
  4. namedtuple
  5. enum.Enum ( Included in Python 3.4 above )

defaultdict

I use defaultdict More , And dict Different types , You don't need to check key Whether there is , So we can do this :

from collections import defaultdict
colours = (
    ('Yasoob', 'Yellow'),
    ('Ali', 'Blue'),
    ('Arham', 'Green'),
    ('Ali', 'Black'),
    ('Yasoob', 'Red'),
    ('Ahmed', 'Silver'),
)
favourite_colours = defaultdict(list)
for name, colour in colours:
    favourite_colours[name].append(colour)
print(favourite_colours)

Operation output

# defaultdict(<type 'list'>,

#   {'Arham': ['Green'],

#    'Yasoob': ['Yellow', 'Red'],

#    'Ahmed': ['Silver'],

#    'Ali': ['Blue', 'Black']

# })

Another important example is : When you assign nested values to a key in a dictionary , If this key doesn't exist , Will trigger keyError abnormal . defaultdict Allow us to bypass this problem in a smart way .

First of all, I share a use dict Trigger KeyError Example , Then provide a use defaultdict Solutions for .

problem :

some_dict = {}
some_dict['colours']['favourite'] = "yellow"

## Abnormal output :KeyError: 'colours'

Solution :

import collections
tree = lambda: collections.defaultdict(tree)
some_dict = tree()
some_dict['colours']['favourite'] = "yellow"

Running normally

You can use it. json.dumps Print out some_dict, for example :

import json
print(json.dumps(some_dict))

  Output : {"colours": {"favourite": "yellow"}}

Counter

Counter It's a counter , It can help us count certain data . For example, it can be used to calculate how many colors everyone likes :

from collections import Counter
colours = (
    ('Yasoob', 'Yellow'),
    ('Ali', 'Blue'),
    ('Arham', 'Green'),
    ('Ali', 'Black'),
    ('Yasoob', 'Red'),
    ('Ahmed', 'Silver'),
)
favs = Counter(name for name, colour in colours)
print(favs)

## Output :

## Counter({

##  'Yasoob': 2,

##  'Ali': 2,

##  'Arham': 1,

##  'Ahmed': 1

## })

We can also use it to count a file , for example :

with open('filename', 'rb') as f:
    line_count = Counter(f)
print(line_count)

deque

deque Provides a double ended queue , You can start from scratch / Add or remove elements at both ends of the tail . To use it , First of all, we should start from collections Import deque modular :

from collections import deque

Now? , You can create a deque object .

d = deque()

Its usage is like python Of list, And provides a similar method , for example :

d = deque()
d.append('1')
d.append('2')
d.append('3')
print(len(d))

## Output : 3

print(d[0])

## Output : '1'

print(d[-1])

## Output : '3'

You can take it out from both ends (pop) data :

d = deque(range(5))
print(len(d))

## Output : 5

d.popleft()

## Output : 0

d.pop()

## Output : 4

print(d)

## Output : deque([1, 2, 3])

We can also limit the size of this list , When the limit you set is exceeded , Data will be squeezed out from the other end of the queue (pop).

The best explanation is to give an example :

d = deque(maxlen=30)

Now when you insert 30 When the data , The leftmost data will be deleted from the queue .

You can also extend the data in the queue from either end :


d = deque([1,2,3,4,5])
d.extendleft([0])
d.extend([6,7,8])
print(d)

## Output : deque([0, 1, 2, 3, 4, 5, 6, 7, 8])

namedtuple

You may already be familiar with tuples .

A tuple is an immutable list , You can store a sequence of data , It and named tuples

(namedtuples) Very much like , But there are several key differences .

The main similarity is that it doesn't look like a list , You can't modify data in tuples . In order to get the data in the tuple , You need to use integers as indexes :


man = ('Ali', 30)
print(man[0])

## Output : Ali

Um. , that namedtuples What is it? ? It turns tuples into a container for simple tasks . You don't have to use an integer index to access a namedtuples The data of . You can be like a dictionary (dict) Same visit namedtuples, but namedtuples It's immutable .

from collections import namedtuple
Animal = namedtuple('Animal', 'name age type')
perry = Animal(name="perry", age=31, type="cat")
print(perry)

## Output : Animal(name='perry', age=31, type='cat')

print(perry.name)

## Output : 'perry'

Now you can see , We can access... By name namedtuple Data in . Let's continue to analyze it . A named tuple (namedtuple) There are two required parameters . They are tuple names and field names .

In the example above , Our tuple name is Animal, The field name is 'name','age' and 'type'.

namedtuple Make your tuples self documenting . You can easily understand what the code does at a glance .

You don't have to use an integer index to access a named tuple , This makes your code easier to maintain .

and ,namedtuple There is no object dictionary for each instance of , So they're light , Compared with ordinary tuples , No more memory required . This makes them faster than dictionaries .

However , Remember that it is a tuple , The attribute value is namedtuple China is immutable , So the following code doesn't work :

from collections import namedtuple
Animal = namedtuple('Animal', 'name age type')
perry = Animal(name="perry", age=31, type="cat")
perry.age = 42

## Output :

## Traceback (most recent call last):

## File "", line 1, in

## AttributeError: can't set attribute

You should use named tuples to make code self documenting , They are backward compatible with ordinary tuples , This means that you can use both integer indexes , You can also use a name to access namedtuple:

from collections import namedtuple
Animal = namedtuple('Animal', 'name age type')
perry = Animal(name="perry", age=31, type="cat")
print(perry[0])

## Output : perry

Last , You can convert a named tuple into a dictionary , Usage is as follows :

from collections import namedtuple
Animal = namedtuple('Animal', 'name age type')
perry = Animal(name="Perry", age=31, type="cat")
print(perry._asdict())

## Output : OrderedDict([('name', 'Perry'), ('age', 31), ...

enum.Enum (Python 3.4+)

Another useful container is enumerating objects , It belongs to enum modular , Exist in Python 3.4 In the above version ( At the same time as an independent PyPI package enum34 For the old version ).Enums( Enumeration type ) It's basically a way of organizing things .

Let's review the previous 'Animal' Examples of named tuples .

It has one type Field , The problem is ,type Is a string .

So here comes the question , In case the programmer enters Cat, Because he pressed Shift key , Or you enter 'CAT', even to the extent that 'kitten'?

Enumerations can help us avoid this problem , By not using strings . Consider the following example :

from collections import namedtuple
from enum import Enum
class Species(Enum):
    cat = 1
    dog = 2
    horse = 3
    aardvark = 4
    butterfly = 5
    owl = 6
    platypus = 7
    dragon = 8
    unicorn = 9

    # By analogy

    # But we don't want to close ⼼ Same as ⼀ Age of species , So we can use an alias    

kitten = 1 # ( translator's note : A baby cat )
    puppy = 2 # ( translator's note : A young dog )
Animal = namedtuple('Animal', 'name age type')
perry = Animal(name="Perry", age=31, type=Species.cat)
drogon = Animal(name="Drogon", age=4, type=Species.dragon)
tom = Animal(name="Tom", age=75, type=Species.cat)
charlie = Animal(name="Charlie", age=2, type=Species.kitten)

Now? , We do some tests :

 charlie.type == tom.type

True

It is not so easy to make mistakes , We must be more specific , And we should only use the defined enumeration types .

There are three ways to access enumeration data , For example, the following methods can obtain 'cat' Value :

Species(1)
Species['cat']
Species.cat

This is just a quick browse collections Module introduction , I suggest you read the official document at the end of this article .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved