学习第五天(Python序列)

Python学习之序列

字符串、列表和元组作为序列
内置函数用法

1.字符串、元组、列表作为序列的一些共同特点
1)都可以通过索引访问其元素,举例说明

>>> str = 'I love FishC.com'
>>> str[0]
'I'
>>> list = [1,2,3,4]
>>> list[1]
2
>>> turple1 = (1,2,3,4)
>>> turple1[1]
2
2)默认索引值都是从0开始
当然,在python中元素负值索引。
3)通过分片方式可以得到多个元素组成的新的序列类型,举例说明
>>> str1 = "I love FishC.com"
>>> str1[:10]
'I love Fis'
>>> list1 = ['小甲鱼','醉酒青牛',123]
>>> list1[1:]
['醉酒青牛', 123]
4)有许多共同的操作符,例如重复操作符*,拼接操作符+,成员操作符in not in等
2、 序列的内置函数
1)list()函数
    list() -> new empty list
    list(iterable) -> new list initialized from iterable's items
将一个可迭代对象强制转换为列表,如果没有参数时,返回一个空列表,举例说明
>>> str1 = 'I love FishC.com'
>>> list1 = list(str1)
>>> list1
['I', ' ', 'l', 'o', 'v', 'e', ' ', 'F', 'i', 's', 'h', 'C', '.', 'c', 'o', 'm']
>>> tuple1 =(1,2,3,4)
>>> list2 = list(tuple1)
>>> list2
[1, 2, 3, 4]
2)tuple()函数,将可迭代对象转换为元组,举例说明
>>> list1 = ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'F', 'i', 's', 'h', 'C', '.', 'c', 'o', 'm']
>>> tuple1 = tuple(list1)
>>> tuple1
('I', ' ', 'l', 'o', 'v', 'e', ' ', 'F', 'i', 's', 'h', 'C', '.', 'c', 'o', 'm')
3)str()函数,将可迭代对象强制转换为字符串
class str(object)
|  str(object='') -> str
|  str(bytes_or_buffer[, encoding[, errors]]) -> str
一般情况下,我们可以将任何一个数据类型强制转换为字符串
>>> m =5
>>> str1 = str(m)
>>> str1
'5'
len()函数
len(module, object)
Return the number of items of a sequence or mapping.
4)len()函数,统计一个可迭代对象的长度并返回长度值,举例说明
>>> str1 = 'I love FishC.com'
>>> len(str1)
16
>>> list1 = [1,3,7]
>>> len(list1)
3
>>> tuple1= (1,2,3,4)
>>> len(tuple1)
4
5)max()和min()函数,比较可迭代对象内元素最大值和最小值并返回,要求所有元素类型必须一致,举例说明
>>> list1 = [12,56,78,56.1]
>>> max(list1)
78
>>> min(list1)
12
>>> str1 = '1234567890'
>>> max(str1)
'9'
>>> min(str1)
'0'
6)sum()函数,将序列内所有元素值求和并给出,这里面要求序列内元素数据类型一致且为数值类型
sum(iterable[, start]) -> value
    Return the sum of an iterable of numbers (NOT strings) plus the value
    of parameter 'start' (which defaults to 0).  When the iterable is
    empty, return start.
返回可迭代对象内的所有元素的和,如果start赋值的话再加上start的值,当可迭代对象为空,则返回start的值,举例说明
>>> list1 = [1,5,9]
>>> sum(list1)
15
>>> sum(list1,5)
20
>>> list2 = list()
>>> sum(list2)
0
>>> sum(list2,1)
1
7)sorted()函数
sorted(iterable, key=None, reverse=False) --> new sorted list
对可迭代对象内的所有元素进行排序,默认从小到大,举例说明
>>> list1 = [1,7,3,8,3,2,16,9]
>>> sorted(list1)
[1, 2, 3, 3, 7, 8, 9, 16]
>>> sorted(list1,reverse=True)
[16, 9, 8, 7, 3, 3, 2, 1]
8)reversed()函数

| reversed(sequence) -> reverse iterator over values of the sequence
| Return a reverse iterator
将可迭代对象中所有元素转置并返回新的对象,举例说明

>>> list1 = [1,2,3]
>>> reversed(list1)
<list_reverseiterator object at 0x023EFF30>
>>> list(reversed(list1))
[3, 2, 1]
9)enumerate()函数

enumerate(iterable[, start]) -> iterator for index, value of iterable
|
| Return an enumerate object. iterable must be another object that supports
| iteration. The enumerate object yields pairs containing a count (from
| start, which defaults to zero) and a value yielded by the iterable argument.
| enumerate is useful for obtaining an indexed list:
| (0, seq[0]), (1, seq[1]), (2, seq[2]), …
将可迭代对象的每一个元素与其对应的索引值组成一个元组并返回这个由元组组成的可迭代对象,举例说明

>>> list1 = ['小狗狗','小猫猫','小牛牛']
>>> enumerate(list1)
<enumerate object at 0x023C0F80>
>>> list(enumerate(list1))
[(0, '小狗狗'), (1, '小猫猫'), (2, '小牛牛')]
zip()函数

| zip(iter1 [,iter2 […]]) –> zip object
|
| Return a zip object whose .next() method returns a tuple where
| the i-th element comes from the i-th iterable argument. The .next()
| method continues until the shortest iterable in the argument sequence
| is exhausted and then it raises StopIteration.
将多个可迭代对象中对应的元素组成一个元组,并返回一个可迭代对象,举例说明

>>> list1 = [1,2,3,4]
>>> list2 = ['a','b','c','d']
>>> tuple1 = ('小狗狗','小猫猫','小牛牛')
>>> zip(list1)
<zip object at 0x018BC7B0>
>>> list(zip(list1))
[(1,), (2,), (3,), (4,)]
>>> list(zip(list1,list2))
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
>>> list(zip(list1,list2,tuple1))
[(1, 'a', '小狗狗'), (2, 'b', '小猫猫'), (3, 'c', '小牛牛')]
3、 序列的内置函数与元组、列表的内置函数最大区别是,序列的内置函数的参数一般是序列

例如sorted()是序列的一个内置函数,而sort()为列表、元组的内置函数,通过例子比较其区别:

>>> list1 = [1,3,2,7]                #定义列表类序列
>>> sorted(list1)                    #通过对序列本身进行排序,获取新的序列
[1, 2, 3, 7]
>>> list1                                 #list1本身并没有改变
[1, 3, 2, 7]
>>> list1.sort()                       #使用列表list1内置函数,改变其本身
>>> list1
[1, 2, 3, 7]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值