
下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、第七章、 數據結構python中有三種內建的數據結構 列表、元組和字典。 1) lists列表 , 列表是序列的一種 shoplist = 'apple', 'carrot', 'banana' print shoplist 'apple', 'carrot', 'banana' shoplist.append('orange') 末尾加入一個 print shoplist 'apple', 'carrot', 'banana',
2、39;orange' shoplist.insert(2, 'flint') 指定位置插入 print shoplist 'apple', 'carrot', 'flint', 'banana', 'orange' shoplist.reverse() 反轉 print shoplist 'orange', 'banana', 'flint', 'carrot', 'apple' shoplist.pop() 默
3、認從末尾刪除 print shoplist 'orange', 'banana', 'flint', 'carrot' shoplist.() 正向排序 print shoplist 'banana', 'carrot', 'flint', 'orange' del shoplist1 指定位置刪除,等于shoplist.pop(1) print shoplist 'banana', 'flint', 'orange'
4、shoplist.tend('database', 'egg') 末尾加入多個 print shoplist 'banana', 'flint', 'orange', 'database', 'egg' shoplist.remove('banana') 刪除指定項 print shoplist 'flint', 'orange', 'database', 'egg' 通過help(list)獲得完整的學
5、問。 列表解析表達式 從一個已有的列表導出一個新的列表。 vec = 2, 4, 6 print (3 * x for x in vec) generator object gen at 0x00e7d300 print list(3 * x for x in vec) 6, 12, 18 print 3 * x for x in vec 6, 12, 18 print 3 * x for x in vec if x 3 12, 18 print 3 * x for x in vec if x 2 print x, x*2 for x in vec 2, 4, 4, 16, 6, 36 m =
6、 1, 2, 3, 4, 5, 6, 7, 8, 9 print row1 for row in m 2, 5, 8 print row1 for row in m if row1 % 2 = 0 2, 8 print x + y for x in 'abc' for y in 'mn' 'am', 'an', 'bm', 'bn', 'cm', 'cn' 處理大型矩陣用法開源numpy系統 nesting嵌套 m = 1, 2, 3, 4, 5, 6, 7, 8,
7、9 print m 1, 2, 3, 4, 5, 6, 7, 8, 9 print m0 1, 2, 3 print m12 6 列表解析 list(map(m, m) 6, 15, 24 sum(row) for row in m (24, 6, 15) x: ord(x) for x in 'spabm' 'a': 97, 'p': 112, 's': 115, 'b': 98, 'm': 109 索引操作符 索引操作符,下標操作,從0開頭,支持反向索引,從-1開頭 sp = 'a
8、9;, 'b', 'c', 'd' sp0, sp1, sp2, sp3 ('a', 'b', 'c', 'd') sp-4, sp-3, sp-2, sp-1 ('a', 'b', 'c', 'd') name = 'swaroop' len(name) 7 name-1 'p' namelen(name) - 1 'p' 切片操作符xi:j 切片(slice)操作符,序
9、列名跟方括號,方括號中有一對可選的數字,并用冒號分割。數是可選的,而冒號是必需的。 xi:j,取出在x中從偏移為i,直到但不包括j的內容 shoplist = 'a', 'b', 'c', 'd' shoplist1:3 'b', 'c' shoplist2: 'c', 'd' shoplist1:-1 'b', 'c' shoplist: 'a', 'b', 'c', 'd
10、9; name = 'swaroop' name1:3 wa,不包括r! name2: aroop name1:-1 waroo name: swaroop name * 2 swaroopswaroop 三元切片操作符xi:j:k xi:j = xi:j:1 s = "abefghijklmn" print s1:10:2 bhj print s:2 acegikm print s:-2 nljhfdb 參考 shoplist = 'apple', 'mango', 'carrot', 'banana
11、' mylist = shoplist mylist is just another name pointing to the same object! del shoplist0 print shoplist 'mango', 'carrot', 'banana' print mylist 'mango', 'carrot', 'banana' mylist = shoplist: make a copy by doing a full slice del mylist0 remove f
12、irst item print shoplist 'mango', 'carrot', 'banana' print mylist 'carrot', 'banana' 假如要復制列表或者序列或者對象,那么你必需用法切片操作符來取得拷貝。記住列表的賦值語句不創建拷貝 淺拷貝深拷貝 淺拷貝(1)徹低切片操作:;(2)利用工廠函數,如list(),dict();(3)用法copy模塊的copy函數 深拷貝(1)用法copy模塊的deepcopy()函數 模擬堆棧 stack = def pushit(): stack.
13、append(raw_input("enter new sing:").strip() def popit(): if len(stack) = 0: print 'can not pop from an empty stack!' ee: print 'remov ', stack-1, '' stack.pop() def viewstack(): print stack cmds = 'u':pushit, 'o':popit, 'v':viewstack def show
14、menu(): pr = """p(u)sh p(o)p (v)iew (q)uit enter choice:""" while true: while true: try: choice = raw_input(pr).strip()0.lower() except (eoferror, keyboardinterrupt, indexerror): choice = 'q' print 'nyou picked: %s' %choice if choice not in 'uovq'
15、;: print 'inval option, try again' else: break if choice = 'q': break cmdschoice() if _name_ = '_main_': showmenu() 2) tuples元組(,) 元組是不行變的 即不能修改。元組通過圓括號中用逗號分割的項目定義。 zoo = ('w', 'e', 'p') new_zoo = ('m', 'd', zoo) print zoo ('w',
16、 'e', 'p') print new_zoo ('m', 'd', ('w', 'e', 'p') print new_zoo2 ('w', 'e', 'p') print new_zoo22 p new_zoo1 = 'x' typeerror: 'tuple' object does not support item assignment 元組最通常的使用是用在打印語句中 age = 22 name = 'swaroop' print '%s is %d years old' % (name, age) print 'why is %s playing with that python?' % name print語句用法跟著%符號的項目元組的字符串。定制輸出滿足某種特定的格式。定制可以是%s表示字符串或%d表示整數。 3) dictionaries字典k:v 鍵值對:d = key1 : value1, key2 : value2 rec
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 畜禽生產試卷B及答案
- 校長合同協議書怎么寫
- 智能物流技術的優勢與挑戰
- 維修采購合同協議書范本
- 農村房屋合同協議書范本
- 塑料生產加工合同協議書
- 2025年疾控中心實驗室大樓項目可行性研究報告(編制大綱)
- 2025年中國鐵碳填料項目投資計劃書
- 無錫烘焙食品項目商業計劃書參考范文
- 青少年托管合同協議書
- 江蘇省南通市海門市海門中學2025屆高考物理四模試卷含解析
- 2025年中考物理模擬考試卷(帶答案)
- 希沃白板5培訓知識點
- deepseek的使用技巧與實際應用培訓課件
- 污水處理站設備采購及配套方案(技術標)
- 攪拌站申請書
- 抖店運營流程
- 印刷廠印刷安全事故應急預案
- 《西安市建筑工程安全生產標準化圖冊(2023版)》
- 光伏發電監理規劃
- 學校教師培訓與發展計劃的國際比較研究
評論
0/150
提交評論