马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
序列- list()
把一个可迭代对象转换为列表 >>> a = list()
>>> a
[]
>>> b = 'This is a string'
>>> b = list(b)
>>> b
['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g']
>>> c = (1,1,2,3,5,8,13,21,34)
>>> c = list(c)
>>> c
[1, 1, 2, 3, 5, 8, 13, 21, 34]
- tuple()
把一个可迭代对象转换为元组 - str(obj)
将obj对象转换为字符串 - max()
返回序列或者参数集合中的最大值,但必须保证序列或者参数的数据类型一致 - min()
返回序列或者参数集合中的最小值,但必须保证序列或者参数的数据类型一致 - sum():sum参数的数据类型必须一致切位整数或者浮点数类型
>>> numbers = [1,2,5,8]
>>> sum(numbers)
16
>>> sum(numbers,4)
20
- sorted()
>>> numbers
[9, 2, 6, 10, 4, 6]
>>> sorted(numbers)
[2, 4, 6, 6, 9, 10]
>>> numbers
[9, 2, 6, 10, 4, 6]
和list.sort()的区别: >>> numbers = [5,2,7,1]
>>> numbers.sort()
>>> numbers
[1, 2, 5, 7]
- reversed()
>>> numbers
[6, 4, 10, 6, 2, 9]
>>> reversed(numbers)
<list_reverseiterator object at 0x03CC69F0>
>>> numbers
[6, 4, 10, 6, 2, 9]
>>> list(reversed(numbers))
[9, 2, 6, 10, 4, 6]
和list.reverse()的区别: >>> numbers
[6, 4, 10, 6, 2, 9]
>>> numbers.reverse()
>>> numbers
[9, 2, 6, 10, 4, 6]
- enumerate()
>>> numbers
[9, 2, 6, 10, 4, 6]
>>> enumerate(numbers)
<enumerate object at 0x03CD9800>
>>> list(enumerate(numbers))
[(0, 9), (1, 2), (2, 6), (3, 10), (4, 4), (5, 6)]
- zip()
>>> a = [1,2,3,4,5,6,7,8]
>>> b = [1,2,3,4]
>>> zip(a,b)
<zip object at 0x03CD9058>
>>> list(zip(a,b))
[(1, 1), (2, 2), (3, 3), (4, 4)]
|