好比說我要整理一個近期熱映的電影列表:python
movies = ["venom", "My Neighbor Totor", "Aquaman"] print(movies) # ['venom', 'My Neighbor Totor', 'Aquaman'] print(len(movies)) # 3 print(movies[1]) # My Neighbor Totor
列表很像數組,但功能超越數組。列表都是從0開始的,python中列表無需事先聲明類型。數組
從列表後面加上一個新的元素,好比說加個「無名之輩」,是append
方法。app
movies.append('A Cool Fish') print(movies) # ['venom', 'My Neighbor Totor', 'Aquaman','A Cool Fish']
刪除列表最後一個元素:pop
方法。模塊化
movies.pop() print(movies) # ['venom', 'My Neighbor Totor']
兩個列表相銜接,用的是extend
方法。函數
movies.extend(['Dying To Survive', 'Detective Conan']) print(movies) # ['venom', 'My Neighbor Totor', 'Aquaman', 'Dying To Survive', 'Detective Conan']
我想在某個條件下刪除一個元素,同時加上一個元素,涉及remove
和insert
方法。學習
movies.remove('venom') print(movies) # ['My Neighbor Totor', 'Aquaman'] movies.insert(len(movies),'venom') print(movies) # ['My Neighbor Totor', 'Aquaman','venom']
python中列表能夠聽任何類型的數據。code
處理列表須要各類迭代方法。是時候用for...in循環了模塊化開發
for movie in movies: print(movie) # venom # My Neighbor Totor # Aquaman
同理while循環也是能夠的對象
count=0 while count<len(movies): print(movies[count]) count+=1
通常都推薦for循環blog
列表固然也能嵌套,咱們能夠用isinstance
方法檢測之:
movies = ["venom", ["My Neighbor Totor", "Aquaman"]] print(isinstance(movies,list)) # True
若是我想把這個嵌套列表單獨打印出來,能夠這麼操做
movies = ["venom", ["My Neighbor Totor", "Aquaman"]] for movie in movies: if isinstance(movie, list): for _movie in movie: print(_movie) else: print(movie) # venom # My Neighbor Totor # Aquaman
給這個列表再加一層:
movies = ["venom", ["My Neighbor Totor", ["Aquaman"]]]
用上節來處理多層嵌套,會致使大量重複而不優雅的代碼。
首先使用函數讓代碼更加優雅:
movies = ["venom", ["My Neighbor Totor", ["Aquaman"]]] def flatten(_list): if(isinstance(_list, list)): for _item in _list: flatten(_item) else: print(_list) flatten(movies)
爲了鞏固所學:再舉一個經過遞歸生成斐波拉契數列第N項的例子:
斐波那契數列(Fibonacci sequence),指的是這樣一個數列:一、一、二、三、五、八、1三、2一、3四、……在數學上,斐波納契數列以以下被以遞推的方法定義:F(1)=1,F(2)=1, F(3)=2,F(n)=F(n-1)+F(n-2)(n>=4,n∈N*)
def fib(n): if n < 1: return 'error' if n == 1 or n == 2: return 1 else: return fib(n-1)+fib(n-2) print(fib(6)) # 8
雖然很簡潔,可是n>100就報錯了。由於python的遞歸支持100層
把上一章的flatten函數單獨用base.py存起來,它就打包爲一個模塊。
除了以前提到的,用#
註釋,還能夠用三重"""
做爲python的註釋。
再同一個命名文件夾下從新建立一個app.py文件:
import base as util movies = ["venom", ["My Neighbor Totor", ["Aquaman"]]] print(util.flatten(movies)) # 預期結果
先學習如下內置的方法(BIF)
>>> id(b) 140588731085608
'5'
轉化爲5
>>>range(10) # 從 0 開始到 10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(1, 11) # 從 1 開始到 11 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> range(0, 10, 3) # 步長爲 3 , 迭代終點不超過10 [0, 3, 6, 9]
>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter'] >>> list(enumerate(seasons)) [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')] >>> list(enumerate(seasons, start=1)) # 下標從 1 開始 [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
扁平化打印只能看到每一個數組最小的元素,考慮用縮進來體現彼此的關係。那怎麼作呢?
提示:用range方法實現。
事實上在打印時只須要知道每次迭代的深度,就好處理了。所以須要引入第二個參數
# base.py def flatten(_list,level=0): if(isinstance(_list, list)): for _item in _list: flatten(_item,level+1) else: for step in range(level): print("\t", end='') print(_list)
# app.py import base as util movies = ["venom", ["My Neighbor Totor", ["Aquaman"]], ["My Neighbor Totor", ['000',["Aquaman"]]], 'aaa', ['Aquaman'],'sadas'] print(util.flatten(movies))
效果出來了。但還有不滿意的地方。若是要兼容過去的寫法怎麼辦?
def flatten(_list, count=False, level=0): if(isinstance(_list, list)): for _item in _list: flatten(_item,count,level+1) else: if count: for step in range(level): print("\t", end='') print(_list) else: print(_list)
調用時默認就是不縮進。