python元组和列表的作用
这篇文章主要介绍“python元组和列表的作用”,在日常操作中,相信很多人在python元组和列表的作用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”python元组和列表的作用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联建站主要从事网站建设、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务邓州,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
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' File " ", 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. (END)...skipping... Help on built-in function cmp in module __builtin__: cmp(...) cmp(x, y) -> integer Return negative 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 "", 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']]
到此,关于“python元组和列表的作用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
新闻标题:python元组和列表的作用
网址分享:http://scyanting.com/article/phoghs.html