The method commonly used built-in module

A: collection module

Role: on the common data types and derived some new data types

  (1) nametuple (named tuple):

    (1) inside a conventional tuples are stored as dictionary data but does not like to which data will be described

    (2) may be described tuple named tuples data inside

E.g:

from   Collections Import namedtuple 

# Point = namedtuple ( 'coordinates', [ 'x', ' y']) # parameter is a parameter descriptive information for the two iterables 
# P = Point (1,2) # uploaded parameter equal to the parameter must contain in iterable 
# Print (P) # coordinates (X =. 1, Y = 2) 

Card = namedtuple ( ' poker ' , [ ' Color ' , ' Number ' ]) 
P = Card ( ' ' , ' A ' )
 Print (P)                             # cards (color = '♠', number = 'A'

 

  (2) deque (Dual Stack Queue):

     (1) dual-stack queue 

     (2) which can be bi-directional add to delete list elements

E.g:

from collections import deque
# 
number = deque([1,2,3,4])
number.append(66)
print(number)       # deque([1, 2, 3, 4, 66])

number = deque([1,2,3,4])
number.appendleft(66)
print(number)       # deque([66, 1, 2, 3, 4]) 头部添加数据
# 
number = deque([1,2,3,4])
number.pop()
print(number)       # deque([1, 2, 3])

number = deque([1,2,3,4])
number.popleft()
print(number)       # Qeque ([2,. 3,. 4]) 
#  
Number = the deque ([1,2,3,4 ]) 
number.insert ( . 1, ' Six ' )
 Print (Number)         # the deque ([. 1, 'Six', 2,. 3,. 4]) 
'' ' 
 the PS: 
(. 1) dual-stack queue should support only end-value index insertion operation 
(2) deque special bit 
# ' ''
deque queue dual stack

 

  (3) orderdict (ordered dictionary):

    (1) A common dictionary are unordered

    (2) ordered dictionary to determine the order of key_value

E.g:

from collections import OrderedDict

user = OrderedDict({'name':'SR','age':18})

print(user)    # OrderedDict([('name', 'SR'), ('age', 18)])

 

  (4)defaultdict

    (1): If the dictionary does not exist can be returned without being given a default value

E.g:

from collections import defaultdict

values = [11, 22, 33,44,55,66,77,88,99,90]

my_dict = defaultdict(list)   # 后续key所对应的value都是列表

for value in  values:
    if value>66:
        my_dict['k1'].append(value)
    else:
        my_dict['k2'].append(value)
print(my_dict)      # defaultdict(<class 'list'>, {'k2': [11, 22, 33, 44, 55, 66], 'k1': [77, 88, 99, 90]})

PS:

(1) When elements which unlike fromkey just a list of key-value pairs are added to the same list

(2) create a list which is created dynamically and different key corresponding to different lieb

 

  (5)counter

    (1) which will be stored in the dictionary of key-value pairs

    (2) key which is a key element of the value for the number of occurrences of the container in the container

E.g:

from collections import Counter

s =Counter ('asdasdadsasdasd')
print(s) # Counter({'a': 5, 's': 5, 'd': 5}

 

Guess you like

Origin www.cnblogs.com/SR-Program/p/11209337.html