python-3_ Function _ anonymous function _ Regular _yield_ Iterator _ serialization _ built-in function _ module

Source: Internet
Author: User
Tags serialization

L=[' A ', ' B ', ' C ', ' d ']

for (Offset,item) in Enumerate (L):
Print Offset,item

Printed results:

2 #
1 b
2 C
4 D


Iterators:

For I in range (100): Generate list

For I in xrange (100) iterators, faster than range

ReadLines () Xreadlines ()

Example:

def run ():
Print "Test1"
Yield 1 #变成迭代器, the program runs here will stop jumping out of execution. Yield is the equivalent of the return value.
Time.sleep (1)
Print "Test2"
Time.sleep (1)
Print "Test2"
Time.sleep (1)
Print "Test2"
Time.sleep (1)

Task=run ()
Task.next ()
print '--do sth else---'
Task.next () #如果不加本行, it will not execute after yield.


Function:

Lambda function (anonymous function):

def f (x):
Return x**2
g = The notation of the lambda x:x**2 anonymous function

Print map (F,range (10)) the list input function operation, and then return the result
Print map (lambda X:x**2,range (10))

Sorted () Usage: example is to sort the dictionary key (although the dictionary is unordered)

Dic1={3:2,4:3,6:9, ' A ': ' Test ', ' e ': ' FFF ', ' * ': ' $ '}
Print sorted (Dic1.items (), Key=lambda x:x[0])
RGS Kargs


Built-in functions:

A=range (10)
A=iter (a) #把列表变成了迭代器
A.next ()

print [i-I in a if a<5]
Filter (lambda x:x*x,a)



Serialization:
Only strings or binaries can be stored on the hard disk, so dictionaries, lists, etc. cannot be saved.
Pickle is just the type of serialization used in Python.

Pickle.dump (object, file handle) Pickle.load () pickle.dump (dic1,f)
Json.dump () Json.load ()

Regular Expressions:

m = Re.match ("\d", matching object) match is matched from the beginning.
View match to Character: M.group ()

Re.search () Global lookup match

Re.findall () finds all matches to the
Re.split () split by symbol
Re.sub (' \d ', ' | ', character object) Replace the comb with |
Re.sub (' \d ', ' | ', character object, count=2) only replace the first two

Module creation and import:

If __name__== ' __main__ ' active execution will execute, import does not execute

Common modules:
Import SYS
Sys.path
Sys.path
Sys.exit

Import OS
Os.system (' uname ')
Os.mkdir (' MyDir ')
Os.chdir (' ~ ')



python-3_ Function _ anonymous function _ Regular _yield_ Iterator _ serialization _ built-in function _ module

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.