Other common modules python-26- (a)

Foreword

Common modules naturally we used to use in everyday language python modules, such as said earlier, re, but there are more commonly used in the default, we understand the next together.

A, collections module

On the basis of built-in data types (dict, list, set, tuple) on, collections module also provides several types of additional data: Counter, deque, defaultdict, namedtuple OrderedDict and the like.

To learn more about other data types:

  1. namedtuple: generation can use the name to access the content of the element tuple
  2. queue: FIFO queue
  3. deque: deque, from the other side can be quickly and additional objects Release
  4. OrderedDict: ordered dictionary
  5. defaultdict: Dictionary with default values
  6. Counter: a counter for counting the main

1, namedtuple generation can use the name to access the content of the element tuple

# . 1, namedtuple generation may use the name to access the content of the element tuple 
from Collections Import namedtuple 
Point = namedtuple ( ' Point ' , [ ' X ' , ' Y ' , ' Z ' ]) 
P = Point (. 1,. 6,. 5 )
 Print (PX)
 Print (Py)
 Print (PZ)
 Print (P)

2, queue: FIFO queue

# 2, Queue: Queue FIFO 
Import Queue 
Q = Queue.Queue () 
q.put ( 10 ) 
q.put ( . 4 ) 
q.put ( . 6 )
 Print (q.get ())
 Print (q.get ( ))
 Print (q.get ())
 Print (q.get ())       # finally did not get've been waiting, until a new value came in 
Print (q.qsize ())      # to see how much value can be taken

3, dueue deque, from the other side can be quickly and additional objects Release

# . 3, dueue deque, from the other side can be quickly and additional objects introduced 
from Collections Import the deque 
DQ = the deque ([2,. 3 ]) 
dq.append ( . 4)           # from behind the discharge data 
dq.appendleft (1)       # reproducing data from the front 
dq.insert (. 4,. 5)        # index data is inserted 
Print (DQ)
 Print (dq.pop ())        # fetch data from the back 
Print (dq.popleft ())    # fetch data from the front 
Print (DQ )

4, OrderedDict the Key is ordered

# 4、OrderedDict的Key是有序的
from collections import OrderedDict
od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
print(od['a'])
for k in od:
    print(od[k])

5, defaultdict, instances, greater than 55 into k1, k2 otherwise put

from collections import defaultdict
values = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
my_dict = defaultdict(list)
for value in values:
    if value>55:
        my_dict['k1'].append(value)
    else:
        my_dict['k2'].append(value)
print(my_dict)

When using dict, if the referenced Key does not exist, it will be thrown KeyError. If you want the key does not exist, returns a default value, you can use

from collections import defaultdict
d = defaultdict(lambda: 5)
print(d['k'])

6, Counter element as the key, as its counting value

from collections import Counter
c = Counter('zzasdhasidhiohs')
print(c)

Two, time module

Among python, and time relationship with the time module generally has three principal ways:

  • 1. The formatted time string
  • 2. timestamp
  • 3. Structure of time, tuples ()

1, strftime: the formatted time string: date time

# . 1, the strftime: string formatted time: date time 
Import Time
 Print (The time.strftime ( ' % Y-M-% D%% H:% M:% S ' ))
 Print (The time.strftime ( ' % Y-M-% D%% H:% M ' ))
 Print (The time.strftime ( ' %% Y-M-% D ' ))
 Print (The time.strftime ( ' % H:% M ' ))

 2, time stamp: time.time ()

# 2、时间戳:time.time()
import time
print(time.time())

 3, localtime: Structured time, tuples ()

# . 3, localtime: Structured time, tuples () 
Import Time 
loc_time = time.localtime ()
 Print (loc_time)
 Print (loc_time.tm_year)

Timeshift:

1, the structure of the time stamp Conversion Conversion

# 1, the time stamp of the conversion structure conversion 
Import Time 
T = the time.time ()
 Print (time.localtime (T))
 Print (time.gmtime (T))

2, the structure of the time stamp Conversion Conversion

# 2, the structure of the conversion time stamp conversion 
Import Time 
Q = time.localtime ()
 Print (time.mktime (Q))

3, the structure of the string formatting time conversion time

# 3, the structure of the string formatting time conversion time 
Import Time
 Print (the time.strptime ( ' 2020.3.8 ' , ' % Y.% m.% D ' ))
 Print (The time.strftime ( ' %% Y-m -% D% H:% M:% S ' , time.localtime (3.5 billion)))

asctime, ctime conversion:

# The asctime, converting the ctime 
Import Time
 Print (that time.asctime ())    # structured format string rotation time period. 8 22:27:26 2020-Mar the Sun 
Print (time.ctime ())      # timestamp formatting to String time: Sun Mar 8 22:27:26 2020

 Welcome to everyone QQ exchange group will study together: 482 713 805

Guess you like

Origin www.cnblogs.com/gsxl/p/12445109.html