列表/元组/字符串的共同点:
- 都可以通过索引得到每一个元素
- 默认索引从0开始
- 切片方法得到一个范围内的元素的集合
- 有很多共同的操作符(重复操作符/拼接操作符/成员关系操作符)
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:把参数转换成字符串
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) #返回序列或参数集合中的最大值
'v'
>>> max(1,1,2,-3,4) #min()最小值
4
>>> tuple = (1,2,3,4,5)
>>> max(tuple)
5
>>> min(tuple)
1
注解:使用max/min,必须保证数据类型一致
>>> t1 = (1,2,3,4)
>>> sum(t1) #求总和
10
>>> sorted(t1) #排序
[1, 2, 3, 4]
reversed #反转
>>> reversed(t1)
0x7f1c2cb10690> #返回的是一个迭代器对象
>>> list(reversed(t1)) #把迭代器对象转换成list
[4, 3, 2, 1]
enumerate
>>> enumerate(t1)
0x7f1c2cb54aa0> #返回的是一个迭代器对象
>>> list(enumerate(t1))
[(0, 1), (1, 2), (2, 3), (3, 4)] #把迭代器对象转换成list
zip
>>> t1 = (1,2)
>>> t2 = (4,5,6,7,8)
>>> zip(t1,t2)
[(1, 4), (2, 5)]