Basic Python Tutorial 1-8 Chapter Summary

Source: Internet
Author: User
Tags string format

Disclaimer: Some of the code is directly copied from the Daniel Blog, has been marked the link.

1 installation

Future Special
U ' c:\n ' ASCII 8-bit Unicode 16-bit

2 lists and tuples

'. Join (somelist), somelist must be a string sequence
Pop removal list last element pop (0) removes the first
X.reverse () list (reversed (x))
y=x[:] (deep copy)
List.sort (), List.sort (CMP) cancels the CMP parameter in python3.x, and does not support direct function to sort (). You can construct a sort function to pass to the key to implement
Numbers.sort (CMP=CMP)
The use of CMP parameters in #python里方法sort () https://segmentfault.com/q/1010000000405289
Sorted (x). Reverse ()
List.sort (Reverse=true)
Persons.sort (Lambda a,b:a[' age ']-b[' age ')

Last in, first out
STACK=[12,45,67,56,89,23,54]
def popit (num):
Jieguo=[]
While True:
If Len (num) ==0:
Break
Else
Tmp=stack.pop ()
Jieguo.append (TMP)
Print (Jieguo)
Popit (Stack)

In-First-out (FIFO) queuing (queue)
Tmp=stack.pop () Replace with Tmp=stack.pop (0) or insert (0,..)
Or the Deque object of the collection module
Http://www.jb51.net/article/88139.htm
Import Collections
Import threading
Import time
Candle = Collections.deque (xrange (5))
def burn (Direction, Nextsource):
While True:
Try
Next = Nextsource ()
Except Indexerror:
Break
Else
print '%8s:%s '% (direction, next)
Time.sleep (0.1)
print '%8s done '% direction
Return
Left = Threading. Thread (Target=burn, args= (' left ', candle.popleft))
right = Threading. Thread (Target=burn, args= (' right ', Candle.pop))
Left.start ()
Right.start ()
Left.join ()
Right.join ()

http://xiaorui.cc/2014/11/02/python%E4%BD%BF%E7%94%A8deque%E5%AE%9E%E7%8E%B0%E9%AB%98%E6%80%A7%E8%83%BD%E5%8F% 8c%e7%ab%af%e9%98%9f%e5%88%97/
The disadvantage of deque is that remove also has the judgment to get the index, the speed is somewhat slow, because he needs to perform many times deque related data block. Not like list, just do it again.

From collections Import Deque
Import Profile,stat
Import Sys
Import time
T0 = Time.clock ()
Print T0
Qeque=deque ()
def add1 (data):
Qeque.append (data)
Def add2 ():
Qeque.pop ()

big_n=1000000
def seq ():
For I in Range (Big_n):
ADD1 (i)
For I in Range (BIG_N/2):
ADD2 ()
For I in Range (Big_n):
ADD1 (i)

L=[]
def add3 (data):
L.append (data)
Def data4 ():
L.pop (-1)

DEF LSE ():
For I in Range (Big_n):
ADD3 (i)
For I in Range (BIG_N/2):
DATA4 ()
For I in Range (Big_n):
ADD3 (i)

SEQ ()
Print Deque
print ' Queue ', Time.clock ()-T0

3 Using Strings

Working with tuples
string format conversion type

Basic Python Tutorial 1-8 Chapter Summary

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.