這篇文章主要介紹“python元組和列表的作用”,在日常操作中,相信很多人在python元組和列表的作用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”python元組和列表的作用”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
我們擁有十余年網(wǎng)頁設(shè)計(jì)和網(wǎng)站建設(shè)經(jīng)驗(yàn),從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁設(shè)計(jì)師為您提供的解決方案。為企業(yè)提供網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、微信開發(fā)、微信小程序、移動(dòng)網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、等業(yè)務(wù)。無論您有什么樣的網(wǎng)站設(shè)計(jì)或者設(shè)計(jì)方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計(jì)服務(wù)并滿足您的需求。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)in () ----> 1 a + 1 TypeError: cannot concatenate 'str' and 'int' objects In [48]: ‘#’ * ‘a(chǎn)' File " ", line 1 ‘#’ * ‘a(chǎn)' ^ 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. (END)...skipping... Help on built-in function cmp in module __builtin__: cmp(...) cmp(x, y) -> integer Return negative if x y.
元組和列表相似
元組和字符串一樣是不可變的
元組可以存儲(chǔ)一系列的值
元組常在用戶定義的函數(shù)能夠安全的采用一組值的時(shí)候,即被使用的元組的值不會(huì)改變。
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 "", line 1 t. ^ SyntaxError: invalid syntax In [74]: t.count Out[74]: In [75]: t. File " ", line 1 t. ^ SyntaxError: invalid syntax In [76]: help(t.count) --------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) in () ----> 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 "", 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) in () ----> 1 list2 NameError: name 'list2' is not defined In [104]: help list.remove File " ", line 1 help list.remove ^ SyntaxError: invalid syntax In [105]: help list3.remove File " ", line 1 help list3.remove ^ SyntaxError: invalid syntax In [106]: helplist3.remove --------------------------------------------------------------------------- NameError Traceback (most recent call last) in () ----> 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) in () ----> 1 list3.remove(2) ValueError: list.remove(x): x not in list In [113]: list3.index Out[113]: In [114]: list3.index(0) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) in () ----> 1 list3.index(0) ValueError: 0 is not in list In [115]: help list(3) File " ", line 1 help list(3) ^ SyntaxError: invalid syntax In [116]: list3.count() --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () ----> 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) in () ----> 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) in () ----> 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) in () ----> 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']]
到此,關(guān)于“python元組和列表的作用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!