2.18 Weeks Four Notes

Source: Internet
Author: User

Day4

Iterators :

1.

LI=[11,22,33,44,55,66,77]

Count =len (LI)

Start = 0

While Count>start:

Print (Li[start])

Start +=1

With indexes, almost all languages support while

the nature of the 2.for cycle

A. Creating a special thing (iterator = iter ())

B. According to this thing, to manipulate the contents of the list Li

C. The execution iterator returns an object that contains the next method

D. One-to-one access to the values inside Li

obj = iter (LI)

While Ture:

item = obj__next__ ()

Item (item)

When you finish, you will be called stop ... ( the For loop does not cause an error because try except Exception: )

3. Summary:

A.while, index, subscript fetch data, random fetch

B.for, executes the iterator ITER, gets a pair of images--> Executes the next method of the object, in order to take

Generator (very important)

1.python in 2.7

Range, memory immediately created 0-9, if the number is large, eat memory

Xrange (Ten) no numbers in memory

For I in Xrange (10):

First time the memory loop is created i= 0

Memory is created in the second loop memory I=1

。。。。。。。。。。。。。。

Memory Nineth Cycles in memory created I=9

Print (i)

2.

Redis ——— Hash ( hash)

Monitor traffic data for www.baidu.com per session

Structure: Hash(hash)

N1 ——— K1 = v1

N2 ——— K11 = v11

Key value

Server 100G Local computer 10G

Dic.keys () # get all

3. File operation

f = open (' File.log ')

#f. Read () # will be reading the day into memory

For line in F: # __inter__ method for triggering F

Print (line) # Generator, memory consumption problem solved

Def show ():

Yield (line1)

Yield (line2)

Yield (LINE3)

My_f = Show ()

For line in My_f:

Print (line)

4.yield Keywords

-------------

Def show (): # function

Return 123

I = Show()

Print(i)

Output 123

-------------

Def show (): # Generator

Yield 123

I = Show()

Print(i)

Output generator

5. Summary:

Def show ():

Yield (line1)

Yield (line2)

Yield (LINE3)

My_f = Show ()

For line in My_f:

Print (line)

‘’’

Line1

Line2

Line3

’’’’

---running logic

1. If the function returns a special item that contains yield, the function returns a value (containing the next method; must be used with for)

2.for, the function returns a special thing, the internal execution of the iteration, if the yield is encountered , will yield after the value of the jump out of the function and remember the last

At the same time, assign the value to the for i

3. Then continue executing the function, go back to the last execution of the code, continue to execute

---Simple comprehension

1. The return value of the generator must be performed by the iterator

2.yield freezes the state, remembering where it was last executed

Decorative Device

def login (func): func= original show

def wrapper (*arg,**kwargs):

print(' before ')

ret =func (*arg,**kwargs)

print(ret)

print (' after ')

return tet

Return wrapper

Def show ()

Return ' show '

#show Memory Value show() execution function

New_show = login (show)

‘’’

Equivalent

Def new_show ():

ret = Show (*arg,**kwargs)

return ret

‘’’

--------

@login

Def show ()

Return ' show '

Functionally implements a nested

Recursive

Regular expressions

Basic algorithm

Common modules: JSON pickle

2.18 Weeks Four Notes

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.