程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Let python fly: Visually understand python stacks, queues, lambdas, generators, iterators, modules

編輯:Python

Day10 A baseball and a bat with crystal furnace and colorful treasure chest
A baseball and a bat came to the football field,Want to play baseball!But the stadium staff does not agree.With a baseball and a bat.Court staff said,這樣吧,According to our rules to complete the task,We will let you play here.There are two their,A tub
棧:滿足特點 ---> 先進後出),A straight barrel.

(隊列:滿足特點 ---> 先進先出.
調用collections模塊中的deque()Function can obtain a queue object operation steps:
步驟一:導入collections模塊
步驟二:collections.deque() --> 得到一個列表對象
步驟三:Using the queue object call deposit(append)和取(popleft)的函數,完成需求)

你們把5A ball into the straight,And then in turn open the ball down on,And get the parts,組裝起來.And then the other5A ball into the straight,The ball from the other end of the barrel out,Also take out the inside parts together.

import collections
'''
The characteristics of the demonstration queue access data:先進先出
'''
#獲取隊列對象:queue
queue = collections.deque()
print(queue,type(queue))
#向隊列中添加數據(Simulation into the team、Phenomenon of entrance)
queue.append('A'))

A baseball and a bat successfully completed the task,Remove the parts,Formed a small blue lion(lambda [變量參數]:表達式).The little lion head and there is no sign of small camel,它的名字叫lambda(匿名函數(沒有名字的函數)).Its mouth can also contain ball can also contains no(Anonymous function also can define the return value has no return value,Have no parameters of),Can contain all kinds of ball,

(Classification of anonymous functions --> With parameters
1).1個參數
2).多個參數
3).默認參數
4).可變參數)

Also can return things or does not return things.To work in the little lion also can turn in the cage,

匿名函數配合列表對象使用lt = [lambda x:x ** 2,lambda x:x ** 3,lambda x:x ** 4]

In the dictionary to work.

匿名函數配合字典對象使用
key = 'C'
dic1 = {
'A':lambda :2*2,
'B':lambda :3*3,
'C':lambda :4*4
}
print(dic1[key]())

A baseball and a bat like this blue lion,To play baseball with him.But a baseball enough,So they want the administrator to give them more few baseball.The administrator from the control room to press the switch,A transparency furnaces of thing happen to the middle of the court.The head of engraved on the stove fo Yin,In furnace are several fold together crystal ball.

There is no real data generator,所以我們是不能直接使用len()Function to try to get its length;
如果我們這麼做了,錯誤提示:no len()

The bottom of the furnace is a switch to open,Every open only out a crystal ball.

Inside it encapsulates a set of formula/算法,只有等到需要調用/Perform data --》執行next()函數
Will the company calculated data results returned to the program#使用next()Function to get every timegenerator中的內容

當然,Fo Yin teacher all at one time to pull out your crystal ball.

方式二:使用循環處理,This way will not excessive calls the internal expression,不會報錯,比較友好
gen = (x for x in range(5))
for g in gen:
print(g).

When the ball was all out after,No balls to take(#generatorObjects in the data content can only be used by parsing once,不可逆;Once the call is much,直接報錯了).Baseball is not happy,Said that if I were in the other place, there is no ball to play,這不行啊.這時,Administrators to smile happily come up with a big red conch,And said to baseball,You put this big conch in the lion's belly exit,Lion will turn into a furnace and transparent container.As the crystal ball can spit out.

(定義函數,Inside the function you need to use the key wordsyieldTo record the returnedgeneratorThe object's data formula;
Perform or throughnext()函數來實現了
def func(n):
lt = []
for i in range(1,n + 1):
if i % 2 == 0:
lt.append(i)
yield i

A baseball and a bat thanked the administrator,But at the same time also produced a doubt?The crystal ball and what is the difference between a box of ordinary ball?The administrator told baseball,Who can be fo Yin teacher a ball a number of container objects belong to the iteration(凡是可作用於for循環的對象都是Iterable(可迭代對象)類型),Like what beef meatball string,大列表、字典、大餅、Orange car and crystal furnace, etc.But only those who have the export control switch can be an iterator.

1).必須是一個可迭代對象
2).可以被next()函數所調用的
凡是可以作用於next()函數的對象都是Iterator類型,它們表示一個惰性計算的序列

If you want to distinguish the iterator object,Can use the little girl in the laundry list of mushroom a treasure chest(collections.Iterable(類型)配合內置函數isinstance()),The kit has a color butterfly,It can distinguish the container is iterative object.

使用collections模塊中的Iterable類型配合isinstance(obj,type)Built-in function to judgeisinstance(obj,collections.Iterable)

The kit has a color change little dragon,It can distinguish the container is an iterator object.

使用collections模塊中的Iterator類型配合isinstance(obj,type)Built-in function to judgeisinstance(obj,collections.Iterator)

Want to become an iterator object if iteration,Just want to add a switch,Have to please double snake hole in the small white dragon.

Will be iterative object into an iterator object
內置函數:
iter(iterable)
it1 = iter(lt)

Baseball is more listen to more interested in!Then went to the magic mushroom little girl laundry list.Administrator told his magic bundle in twin snakes.A baseball and a bat came to the door of the double snake hole.Found the door has a lot of colorful treasure box is arranged on both sides of David(模塊(model):概念:在python中.py結尾的文件,我們稱為模塊;作用:將類、函數、屬性...Content definition of).Riot points three treasures,One is a double snake hole to have some(.標准庫模塊:安裝完pythonEnvironment of the module,If we want to use these modules,導入即可),Like the rubik's cube(random),Colorful clock(time),Colorful robot(os),Mushroom girl colorful laundry list(collections),Color the apple tree(math).The door of the colorful treasure chest is artificially created(在項目開發過程中,The whole team programmers define their own,可以給自己、Others call),And some are imported from abroad treasure,Want to agree through intelligent housekeeper robot first(打開cmd --> 輸入pip -V(This operation to see whether the installation is completepip)),And then let the bearers carried the treasure into the hole(Install a third-party modules:pip install 模塊名).A detailed description of bearers have treasure(View a third-party modules detailed information:pip show 模塊名),And bearers and zhang all carried into a full list of treasure(Check the installation of all third-party modules:pip list),當然,If don't want to be a treasure can let the bearers to be carried away(Delete a third-party modules:pip uninstall 模塊名).If you want to use what's inside the treasure or treasure,Can directly to treasure,Or some parts of the treasure in to your closed studio.

1).精確導入:
舉例:
import time
from random import randint
from math import pi,
Also can directly import all
2).模糊導入:
舉例:
from math import *
from os import *

在導入的時候,You can also give treasure or what's inside the name,As long as you cover the previous name is ok.

To import the modules or functions、屬性起別名:
使用as關鍵字來實現
【注意】:If once to module、函數、Attribute the alias,The original name is unavailable.
If once to module、函數、Attribute the alias,The original name is unavailable;
from random import randint as r

Some do treasure chest in the treasure do not want to don't call others,With a four iron chain to lock the.

代碼:
if __name__ == '__main__': The role of this code is to don't want to be import loading code defines

Baseball is in the middle of the square hole in the front of the door is seeing a cloth bag hanging in the air,Is associated with the treasures inside the name list.

"包"的概念:package
創建一個python package,Create success is one of the so-calledpython的包;
它的作用:Multiple related module definition in it,方便之後的維護和管理

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved