这篇文章主要介绍“python元组和列表的作用”,在日常操作中,相信很多人在python元组和列表的作用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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']]
到此,关于“python元组和列表的作用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。