Python Full Stack development * 26 Knowledge Point Summary * 180709

Source: Internet
Author: User

Logging Collections Random Module
I. Logging
Low provisioning: Logs cannot be written to file and displayed simultaneously
1. Write files only:
Import Logging
Logger=logging.getlogger () #创建一个对象
fn=logging. Filehandler ("High match. Log", encoding= "Utf-8", mode= "W")
Logger.addhandler (FN)
logging.debug ("debug Message")
logging.info ("info message")
logging.warning ("warning message")
logging.error ("error message")
logging.critical ("critical message")
2. Write file display at the same time
Import Logging
Logger=logging.getlogger () #创建一个对象
fn=logging. Filehandler ("High match. Log", encoding= "Utf-8", mode= "W")
sh=logging. Streamhandler ()
Logger.addhandler (FN)
Logger.addhandler (SH)
logging.debug ("debug Message")
logging.info ("info message")
logging.warning ("warning message")
logging.error ("error message")
logging.critical ("critical message")
3. Setting the display mode
Import Logging
Logger=logging.getlogger ()
fh=logging. Filehandler ("High match. Log", encoding= "Utf-8", mode= "W")
sh=logging. Streamhandler ()
formatter=logging. Formatter ("% (asctime) s-% (name) s-% (levelname) s-% (message) s")
Logger.addhandler (FH)
Logger.addhandler (SH)
Sh.setformatter (Formatter)
Fh.setformatter (Formatter)
logging.debug ("debug Message")
logging.info ("info message")
logging.warning ("warning message")
logging.error ("error message")
logging.critical ("critical message")
4. Setting the level
Import Logging
Logger=logging.getlogger ()
fh=logging. Filehandler ("High match. Log", encoding= "Utf-8", mode= "W")
sh=logging. Streamhandler ()
formatter=logging. Formatter ("% (asctime) s-% (name) s-% (levelname) s-% (message) s")
Logger.setlevel (logging. DEBUG) # No it shows and writes files by default starting from warning
Logger.addhandler (FH)
Logger.addhandler (SH)
Sh.setformatter (Formatter)
Fh.setformatter (Formatter)
Sh.setlevel (logging. DEBUG)
fh.setlevel (logging.info)
logging.debug ("debug Message")
logging.info ("info message")
logging.warning ("warning message")
logging.error ("error message")
logging.critical ("critical message")
Two. Collections
1.namedtuple
From collections Import Namedtuple
Point = Namedtuple ("point", ["X", "Y"])
p=point (10,30) #point (x=10, y=30)
Print (p)
2.deque bidirectional Queue
From collections Import Deque
Q=deque (["A", "B", "C", "D", "E", "F"])
Increase
q.append (666)
q.append (888)
Q.appendleft (111)
q.appendleft (222) #deque ([222, 111, ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', 666, 888])
print (q)
Delete
Q1=deque (["A", "B", "C", "D", "E", "F"])
Q1.pop () # deque ([' A ', ' B ', ' C ', ' d ', ' e '])
Q1.pop () # deque ([' A ', ' B ', ' C ', ' d '])
q1.popleft () # deque ([' B ', ' C ', ' d '])
q1.popleft () # deque ([' C ', ' d '])
Print (Q1)
added: Queue FIFO First principle
stack: Advanced back out
3.OrderedDict
dic = {}
dic[' name '] = ' Alex '
dic[' age '] = ' + '
dic[' sex ' = ' male '
print (DIC)
From collections Import Ordereddict
od=ordereddict ()
od["name"]= "Alex"
od["Age"]= "
print (OD) # ordereddict ([' Name ', ' Alex '), (' Age ', ' + ')])
d = dict (' A ', 1), (' B ', 2), (' C ', 3)])
print (d) # {' A ': 1, ' B ': 2, ' C ': 3}
From collections Import Ordereddict
od1 = ordereddict ([' A ', 1), (' C ', 3), (' B ', 2),])
print (OD1) # ordereddict ([' A ', 1), (' C ', 3), (' B ', 2)])

4.defaultdict
Practice
Method One
L1 = [11,22,33,44,55,77,88,99,90]
dic = {}
For i in L1:
if i<66:
if "Key1" not in DIC:
dic["Key1"]=[]
dic["Key1"].append (i)
Else:
if "Key2" not in DIC:
dic["Key2"]=[]
dic["Key2"].append (i)
print (DIC) # {' Key1 ': [One, one, one, one, and one], ' key2 ': [All in one, one, and one]}
Method Two:
From collections Import Defaultdict
L1 = [11,22,33,44,55,77,88,99,90]
my_dict=defaultdict (list)
my_dict["K1"]
my_dict["K2"]
print (my_dict) #defaultdict (<class ' list ', {' K1 ': [], ' K2 ': []})
For v in L1:
If v <:
my_dict["K1"].append (v)
Else:
my_dict["K2"].append (v)
print (my_dict) # defaultdict (<class ' list '), {' K1 ': [One, one, a, a, a, a], ' K2 ': [[n], [n], [+]}]
Exercise 2
Dic1 = {} #---> dic1={1:5,2:5,3:5.....20:5}
Method One:
For i in range (1,21):
dic1[i]=5
print (DIC1)
Method Two:
Dict={x:5 for x in range (1,21)}
print (dict)
Method Three:
dict1=defaultdict (lambda:5)
For i in range (1,21):
Dict1[i]
print (DICT1)
method Four
Dic2 = Dict.fromkeys (range (1,21), 5)
Fromkeys The first parameter is each taken out and the second parameter is organized into a key-value pair (Pit note: The generated key points to the same object, changing one, and the other changes.)
print (DIC2)

5.Counter
From collections Import Counter
c=counter ("Gdhhhfffddggghhhfff")
D=counter ([1,2,2,2,2,3,3,3,3,4,4,4,4])
E=counter ((6,6,6,6,6,8,8,8,8,8,8,))
B=counter ({"Name": "Alex", "name": "Eve", "name": "Mary"})
print (c) # Counter ({' H ': 6, ' F ': 6, ' G ': 4, ' d ': 3}) returned as a dictionary
print (d) # Counter ({2:4, 3:4, 4:4, 1:1})
print (e) # Counter ({8:6, 6:5})
print (b) # Counter ({' name ': ' Mary '})
three. Random
1.random (+)#0到1之间的小数
2.uniform (1.3)# more than 1 decimals less than 3
3.randint (1,5)# 1=<,=<5 of integers
4.randrange (1,10,2)# 1 to 10 odd Gu Tou regardless of tail
5.choice ([1, "23°c", [4,5])# There must be an indexed data type inside the parentheses
6.sample ([1,2,3,4,5,6,],2)#列表元素任意两个组合.
7.shuffle (item)# Scramble The order of the item

Python Full Stack development * 26 Knowledge Point Summary * 180709

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.