6.序列!序列!
列表/元組/字符串的共同點:
- 都可以通過索引得到每一個元素
- 默認索引從0開始
- 切片方法得到一個范圍內(nèi)的元素的集合
- 有很多共同的操作符(重復操作符/拼接操作符/成員關系操作符)
list():把可迭代對象轉換為列表
help(list)見下: class list(object)| list() -> new empty list| list(iterable) -> new list initialized from iterable's items| | Methods defined here:| | __add__(...)| x.__add__(y) <==> x+y| | __contains__(...)| x.__contains__(y) <==> y in x| | __delitem__(...)| x.__delitem__(y) <==> del x[y]| | __delslice__(...)| x.__delslice__(i, j) <==> del x[i:j]| | Use of negative indices is not supported.| | __eq__(...)【例】 >>> a = list() >>> a [] >>> >>> b = 'I love fishC' >>> b = list(b) >>> b ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'f', 'i', 's', 'h', 'C'] >>>tuple:把可迭代對象轉換成元組
help(tuple)見下: class tuple(object)| tuple() -> empty tuple| tuple(iterable) -> tuple initialized from iterable's items| | If the argument is a tuple, the return value is the same object.| | Methods defined here:| | __add__(...)| x.__add__(y) <==> x+y| | __contains__(...)| x.__contains__(y) <==> y in x| | __eq__(...)| x.__eq__(y) <==> x==y| | __ge__(...)| x.__ge__(y) <==> x>=y| | __getattribute__(...)【例】 >>> a = tuple() >>> a () >>> >>> b = 'I love fishC' >>> b = tuple(b) >>> b ('I', ' ', 'l', 'o', 'v', 'e', ' ', 'f', 'i', 's', 'h', 'C') >>>str:把參數(shù)轉換成字符串
help(str)見下: class str(basestring)| str(object='') -> string| | Return a nice string representation of the object.| If the argument is a string, the return value is the same object.| | Method resolution order:| str| basestring| object| | Methods defined here:| | __add__(...)| x.__add__(y) <==> x+y| | __contains__(...)| x.__contains__(y) <==> y in x| | __eq__(...)| x.__eq__(y) <==> x==y【例】 >>> a = 'I love fishC' >>> str(a) 'I love fishC' >>> >>> len(a) #長度 12 >>> max(a) #返回序列或參數(shù)集合中的最大值 'v' >>> max(1,1,2,-3,4) #min()最小值 4 >>> tuple = (1,2,3,4,5) >>> max(tuple) 5 >>> min(tuple) 1 注解:使用max/min,必須保證數(shù)據(jù)類型一致 >>> t1 = (1,2,3,4) >>> sum(t1) #求總和 10 >>> sorted(t1) #排序 [1, 2, 3, 4]reversed #反轉 >>> reversed(t1) <reversed object at 0x7f1c2cb10690> #返回的是一個迭代器對象 >>> list(reversed(t1)) #把迭代器對象轉換成list [4, 3, 2, 1]enumerate >>> enumerate(t1) <enumerate object at 0x7f1c2cb54aa0> #返回的是一個迭代器對象 >>> list(enumerate(t1)) [(0, 1), (1, 2), (2, 3), (3, 4)] #把迭代器對象轉換成listzip >>> t1 = (1,2) >>> t2 = (4,5,6,7,8) >>> zip(t1,t2) [(1, 4), (2, 5)]總結
- 上一篇: 5.字符串:各种奇葩的内置方法/格式化
- 下一篇: 7.函数def