python元組和列表
In [41]: len(a) Out[41]: 5 In [42]: a+'f' Out[42]: 'abcdef' In [43]: a Out[43]: 'abcde' In [44]: print a abcde In [45]: a * 10 Out[45]: 'abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde' In [46]: '#'*50 Out[46]: '##################################################' In [47]: a + 1 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-47-a1bd27f4633f> in <module>() ----> 1 a + 1 TypeError: cannot concatenate 'str' and 'int' objects In [48]: ‘#’ * ‘a' File "<ipython-input-48-697a37d8b47d>", line 1 ‘#’ * ‘a' ^ SyntaxError: invalid syntax In [49]: 'a' in a Out[49]: True In [50]: a Out[50]: 'abcde' In [51]: 'f' in a Out[51]: False In [52]: 'f' not in a Out[52]: True In [53]: 'f' not in a +'f' Out[53]: False In [54]: max(a) Out[54]: 'e' In [55]: min(a) Out[55]: 'a' In [56]: cmp(a,'abcde') Out[56]: 0 In [57]: cmp(a,'abcdef') Out[57]: -1 In [58]: cmp(a+'g','abcde') Out[58]: 1 In [59]: help(cmp) Help on built-in function cmp in module __builtin__: cmp(...) cmp(x, y) -> integer Return negative if x<y, zero if x==y, positive if x>y. (END)...skipping... Help on built-in function cmp in module __builtin__: cmp(...) cmp(x, y) -> integer Return negative if x<y, zero if x==y, positive if x>y.
元組和列表相似
元組和字串一樣是不可變的
元組可以儲存一系列的值
元組常在使用者定義的函式能夠安全的採用一組值的時候,即被使用的元組的值不會改變。
In [60]: t=('a',1,(1,)) In [61]: t Out[61]: ('a', 1, (1,)) In [62]: t1=(1) In [63]: type(t1) Out[63]: int In [64]: t1=(1,) In [65]: type(t1) Out[65]: tuple In [66]: a Out[66]: 'abcde' In [67]: t =(a,'b','c') In [68]: t Out[68]: ('abcde', 'b', 'c') In [69]: first,second,third=t In [70]: first Out[70]: 'abcde' In [71]: second Out[71]: 'b' In [72]: third Out[72]: 'c' In [73]: t. File "<ipython-input-73-a9f65571ee48>", line 1 t. ^ SyntaxError: invalid syntax In [74]: t.count Out[74]: <function count> In [75]: t. File "<ipython-input-75-a9f65571ee48>", line 1 t. ^ SyntaxError: invalid syntax In [76]: help(t.count) --------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-76-aca5cb18b0a7> in <module>() ----> 1 help(t.count) In [78]: t.count('b') Out[78]: 1 In [79]: t.count('z' ...: ) Out[79]: 0 In [80]: t Out[80]: ('abcde', 'b', 'c') In [81]: t.count(a) Out[81]: 1 In [82]: t.count('a') Out[82]: 0 In [84]: t.index('b') Out[84]: 1 In [85]: t.index('c') Out[85]: 2 In [86]:
列表:
In [86]: list3 =['a',1,(1,),['hello','python']] In [87]: list3 Out[87]: ['a', 1, (1,), ['hello', 'python']] In [88]: len(list3) Out[88]: 4 In [89]: list3[0] Out[89]: 'a' In [90]: list3[0]='b' In [91]: list3 Out[91]: ['b', 1, (1,), ['hello', 'python']] In [92]: list2=[] In [93]: list2.append('linux') In [94]: list2 Out[94]: ['linux'] In [95]: list3+list2 Out[95]: ['b', 1, (1,), ['hello', 'python'], 'linux'] In [96]: (list3+list2)*2 Out[96]: ['b', 1, (1,), ['hello', 'python'], 'linux', 'b', 1, (1,), ['hello', 'python'], 'linux'] In [97]: list3 Out[97]: ['b', 1, (1,), ['hello', 'python']] In [98]: dev list3[-1] File "<ipython-input-98-a37927fbd6ca>", line 1 dev list3[-1] ^ SyntaxError: invalid syntax In [99]: del list3[-1] In [100]: list3 Out[100]: ['b', 1, (1,)] In [101]: list2 Out[101]: ['linux'] In [102]: del list2 In [103]: list2 --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-103-db9b7629a516> in <module>() ----> 1 list2 NameError: name 'list2' is not defined In [104]: help list.remove File "<ipython-input-104-178e53c9bdee>", line 1 help list.remove ^ SyntaxError: invalid syntax In [105]: help list3.remove File "<ipython-input-105-b98070bef357>", line 1 help list3.remove ^ SyntaxError: invalid syntax In [106]: helplist3.remove --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-106-e9ee3419c47a> in <module>() ----> 1 helplist3.remove NameError: name 'helplist3' is not defined In [107]: list3 Out[107]: ['b', 1, (1,)] In [108]: list3.append(1) In [109]: list3 Out[109]: ['b', 1, (1,), 1] In [110]: list3.remove(1) In [111]: list3 Out[111]: ['b', (1,), 1] In [112]: list3.remove(2) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-112-700afa01bf12> in <module>() ----> 1 list3.remove(2) ValueError: list.remove(x): x not in list In [113]: list3.index Out[113]: <function index> In [114]: list3.index(0) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-114-05fab3a45675> in <module>() ----> 1 list3.index(0) ValueError: 0 is not in list In [115]: help list(3) File "<ipython-input-115-81a30b2a31f7>", line 1 help list(3) ^ SyntaxError: invalid syntax In [116]: list3.count() --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-116-d977b332b071> in <module>() ----> 1 list3.count() TypeError: count() takes exactly one argument (0 given) In [117]: list3.remove(1) In [118]: list3 Out[118]: ['b', (1,)] In [119]: help(list3.remove) In [120]: pop() --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-120-de840d9b1602> in <module>() ----> 1 pop() NameError: name 'pop' is not defined In [121]: 'a' in list3; In [122]: 'a' in list3 Out[122]: False In [123]: list3.pop(0) Out[123]: 'b' In [124]: list3.pop(1) --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-124-48a521f74801> in <module>() ----> 1 list3.pop(1) IndexError: pop index out of range In [125]: list3 Out[125]: [(1,)] In [126]: list3.append(4,5) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-126-cd4f2cf121c4> in <module>() ----> 1 list3.append(4,5) TypeError: append() takes exactly one argument (2 given) In [127]: list3.append(4) In [128]: list3.append(5) In [129]: list3 Out[129]: [(1,), 4, 5] In [130]: list3.pop(2) Out[130]: 5 In [131]: help(list3.pop(0)) In [132]: help(list3.pop()) In [133]: help(list3.pop) In [134]: list3 Out[134]: [] In [136]: list3.append('a') In [137]: list3.append(3) In [138]: list3.append(4) In [139]: list3.append(5) In [140]: list3 Out[140]: ['a', 3, 4, 5] In [142]: list3.insert(1,'list1') In [143]: list3 Out[143]: ['a', 'list1', 3, 4, 5] In [150]: list1=[] In [151]: In [151]: list3=[] In [152]: list3.insert(1, list1) In [153]: list3 Out[153]: [[]] In [154]: list3[0].append('abc') In [155]: list3 Out[155]: [['abc']]
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/20893244/viewspace-2220219/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 列表和元組
- python列表(list)和元組(tuple)詳解Python
- Python列表、元組、字典使用Python
- 列表和元組的方法
- Python之列表&元組小練Python
- python list列表基礎(元組)Python
- python_列表——元組——字典——集合Python
- Python 學習之元組列表Python
- Python元組、列表、集合及列表去重操作Python
- python中的列表和元組有什麼區別Python
- python學習筆記:第4天 列表和元組Python筆記
- python資料型別 列表+元組Python資料型別
- python 元組,列表 迴圈遍歷Python
- python如何返回元組,列表或字典的?Python
- 013:列表和元組的應用
- Python 元組,不可變的列表,滾雪球學 PythonPython
- 列表與元組
- python 元組與列表的異同點 1125Python
- Python學習筆記8——列表、字典、元組Python筆記
- Python 列表、元組、字典及集合操作詳解Python
- 【Python】(六)Python資料型別-列表和元組,九淺一深,用得到Python資料型別
- python經典面試題:列表和元組有什麼異同?Python面試題
- Python資料型別(數字,字串,[列表],(元組),{字典:字典值},{列表,列表2})Python資料型別字串
- Python中列表、元組、字典有何區別?Python學習!Python
- Python 選列表 list 還是元組 tuple 的思考Python
- 豬行天下之Python基礎——3.2 列表 & 元組Python
- Python之列表與元組的區別詳解Python
- 三、python的資料型別 列表、元組、字典Python資料型別
- 資料型別· 第1篇《元組和列表的效能分析、命名元組》資料型別
- Python多維列表(元組)合併成一維形式Python
- 列表、元組、字串是有序序列嗎?Python基礎教程字串Python
- python基礎之序列型別的方法——列表&元組Python型別
- Python基礎:資料型別-列表與元組(6)Python資料型別
- 從零開始學Python:第十三課-列表和元組的應用Python
- python3 筆記13.列表元組的切片和支援的運算方法Python筆記
- Python列表及元組的相同點與不同點介紹Python
- 2.列表_元組_字典_集合
- Python元組和字典的拆包Python