python序列

1.重复操作符(*)、拼接操作符(+)、成员关系操作符:(in,not in)

>>> a = [1, 2, 3, 4]
>>> 4 in a
True
>>> 6 not in a
True
>>> 

2.max()、min()

>>> s = ['a', 'b', 'c', 'd', 'e']
>>> max(s)
'e'
>>> """字符串类型比较的是对应ASCII值的大小,大写字母编码值在小写字母之前"""
>>> s = [1, 'a', 'w', 4]
>>> max(s)
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    max(s)
TypeError: '>' not supported between instances of 'str' and 'int'
>>> """数据序列中数据类型不同不能进行比较"""
"""传入空列表会报错"""

>>> s = []
>>> min(s)
Traceback (most recent call last):
  File "<pyshell#94>", line 1, in <module>
    min(s)
ValueError: min() arg is an empty sequence

"""可以同过设置default参数结决"""

>>> min(s, default="屁,啥都没有,怎么找到最小?")
'屁,啥都没有,怎么找到最小?'
"""还可以直接传入参数"""

>>> max(4, 95, 49)
95

3.sum(iterable[,start = 0])

>>> s = [1, 2, 3, 4, 5]
>>> sum(s)
15
>>> sum(s, 10)
25
>>> """返回序列iterable和可选参数start的总和"""
>>> s = [1, 2, 3, 4, 's']
>>> sum(s)
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    sum(s)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> """数据类型不同不能求和"""
>>> s = '153515'
>>> sum(s)
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    sum(s)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> """字符串不能求和"""

4.sorted()


>>> sorted([1, 56555, -99, 5])
[-99, 1, 5, 56555]
>>> """sorted()作用是从小到大排序"""

"""sorted(s)与s.sort()的区别:
sorted()返回全新列表,而sort()对原列表进行排序。
sort()只能用于列表,sorted()用于任何序列

相同点:都支持 key(写函数),reverse参数"""

>>> t = ["FishC", "Apple", "Book", "Banana", "Pen"]
>>> sorted(t)
['Apple', 'Banana', 'Book', 'FishC', 'Pen']

"""这里没有写key,默认是依次比较首字母的编码值"""

>>> sorted(t, key=len)
['Pen', 'Book', 'FishC', 'Apple', 'Banana']

"""这里比较的是len函数的返回结果,注意不加括号,只写函数名"""

5.reversed()

>>> reversed([1,2,3,4,5,-98])
<list_reverseiterator object at 0x0000013B3954BAC0>
>>> list(reversed([1,2,3,4,5,-98]))
[-98, 5, 4, 3, 2, 1]
>>> """reversed()倒序输出,返回迭代器对象,可用list()转换为列表"""

6.enumrate()

>>> num = [4, 52, -4868, 5]
>>> enumerate(num)
<enumerate object at 0x0000013B395669C0>
>>> list(enumerate(num))
[(0, 4), (1, 52), (2, -4868), (3, 5)]
>>> """enumerate()枚举,list(enumerate())返回由每个元素及索引值组成的元组的列表"""

"""enumerate()还有一个start参数,表示从几开始,默认是0"""
>>> num = [4, 52, -4868, 5]
>>> enumerate(num, start=10)
<enumerate object at 0x0000013B3958D600>
>>> list(enumerate(num, start=10))
[(10, 4), (11, 52), (12, -4868), (13, 5)]

7.zip()

>>> a = ['a', 'b', 'c', 'd', 'e', 'f']
>>> b = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 0]
>>> zip(a, b)
<zip object at 0x0000013B377F6DC0>
>>> list(zip(a, b))
[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6)]

"""zip()可传入大于等于2个序列,且长度不要求一样,木桶效应"""


"""要不丢掉每一个元素可使用如下方法:"""
>>> x, y, z = [1, 2, 3], [4, 5, 6], "FishC"
>>> import itertools
>>> zipped = itertools.zip_longest(x, y, z)
>>> list(zipped)
[(1, 4, 'F'), (2, 5, 'i'), (3, 6, 's'), (None, None, 'h'), (None, None, 'C')]

8.id()

        可变序列:

>>> s = [1, 2, 3]
>>> id(s)
1353876821760
>>> s *= 2
>>> s
[1, 2, 3, 1, 2, 3]
>>> id(s)
1353876821760

""""发现这两次调用id函数的值是一样的。
在pyhon每一个对象都有三个属性:唯一标识、类型、值"""

""""发现这两次调用id函数的值是一样的。
在pyhon每一个对象都有三个属性:唯一标识、类型、值
唯一标志在类型被创建时存在,不可以被修改,也不会有重复的值"""

"""id()返回代表该对象唯一标志的整数值"""

"""以上对s进行增量赋值,其内容改变,但id值不变"""

        不可变序列:

>>> t = 1, 2, 3
>>> id(t)
1353876768896
>>> t *= 2
>>> t
(1, 2, 3, 1, 2, 3)

>>> id(t)
1353876461216


"""虽然变量名还是t,却早已物是人非"""

9.is()、not is()

"""检测两个对象的id值是否相等,从而判断是否为同一对象。因此被称为同一性运算符"""

>>> x = "FishC"
>>> y = "FishC"
>>> x is y
True
>>> x = [1, 2, 3]
>>> y = [1, 2, 3]
>>> x is y
False

10.del

         让指定的元素消失

>>> x = "asda"
>>> y = [1, 2, 3]
>>> del x, y
>>> x
Traceback (most recent call last):
  File "<pyshell#63>", line 1, in <module>
    x
NameError: name 'x' is not defined
>>> y
Traceback (most recent call last):
  File "<pyshell#64>", line 1, in <module>
    y
NameError: name 'y' is not defined
>>> 

        删除可变序列中的指定元素

>>> x = [1, 2, 3, 4, 5]
>>> del x[1:4]
>>> x
[1, 5]

"""可用切片语法实现同样的操作"""

>>> x = [1, 2, 3, 4, 5]
>>> x[1:4] = []
>>> x
[1, 5]
>>> x = [1, 2, 3, 4, 5]
>>> del x[::2]
>>> x
[2, 4]

"""从索引值为0开始,步进值为2,依次删除"""

>>> x = [1, 2, 3, 4, 5]
>>> x[::2] = []
Traceback (most recent call last):
  File "<pyshell#76>", line 1, in <module>
    x[::2] = []
ValueError: attempt to assign sequence of size 0 to extended slice of size 3


 """切片语法实现不了此功能"""

        相当于clear()

>>> x = [1, 2, 3, 4, 5]
>>> x.clear()
>>> x
[]




>>> y = [1, 2, 3, 4, 5]
>>> del y[:]
>>> y
[]

11.list()、tuple()、str()

>>> str([1, 2, 3, 4])
'[1, 2, 3, 4]'


"""元组不能省略括号"""
>>> str(1, 2, 3, 4)
Traceback (most recent call last):
  File "<pyshell#88>", line 1, in <module>
    str(1, 2, 3, 4)
TypeError: str() takes at most 3 arguments (4 given)


>>> str((1, 2, 3, 4))
'(1, 2, 3, 4)'
>>> 



"""要生成‘1234’要用join()"""
>>> ''.join(['1', '2', '3', '4'])
'1234'
>>> ''.join(('1', '2', '3', '4'))
'1234'


"""注意参数的每个元素必须是字符串"""
>>> ''.join([1, 2, 3, 4])
Traceback (most recent call last):
  File "<pyshell#90>", line 1, in <module>
    ''.join([1, 2, 3, 4])
TypeError: sequence item 0: expected str instance, int found

12.len()

"""len()有最大承受范围"""

>>> len(range(pow(2, 100)))
Traceback (most recent call last):
  File "<pyshell#102>", line 1, in <module>
    len(range(pow(2, 100)))
OverflowError: Python int too large to convert to C ssize_t

"""最大承受范围对于32位是2**31 -1,对于64位是2**63 -1"""

13.any()、all()

"""any()判断是否存在为真元素,all()判读是否都是为真元素

>>> all([0 ,2, 2])
False
>>> any([0, 1, 2 ])
True

14.map()

"""根据提供的函数对制定的可迭代对象的每个元素进行运算,并将返回运算结果的迭代器"""

>>> mapped = map(ord, "FishC")
>>> list(mapped)
[70, 105, 115, 104, 67]
#对应FishC每一个字符的编码

"""第一个参数传入一个函数名,第二个参数传入一个可迭代对象"""

>>> list(map(pow, [2, 3, 10], [5, 2, 3]))
[32, 9, 1000]

"""这里分别计算2的5次方, 3的2次方, 10的3次方"""

>>> list(map(pow, [2, 3, 10, 34, -3], [5, 2, 3]))
[32, 9, 1000]

"""木桶效应"""

15.filter()

"""根据提供的函数对制定的可迭代对象的每个元素进行运算,
并将运算结果为真的元素,以迭代器的形式返回"""

>>> list(filter(str.islower, "FishC"))
['i', 's', 'h']

"""filter()第一个参数是过滤函数,第二个是待过滤的可迭代对象,过滤可迭代对象的每个元素"""

16.迭代器vs可迭代对象

        —        一个迭代器一定是一个可迭代对象

        —         可迭代对象是可以重复使用,而迭代器则是一次性的

>>> mapped = map(ord, "FishC")
>>> for each in mapped:
	print(each, end=' ')

	
70 105 115 104 67 
>>> list(mapped)
[]
>>> 

        —         iter()将可迭代对象转换为迭代器

>>> x = [1, 2, 3, 4, 5]
>>> y = iter(x)
>>> type(x)
<class 'list'>
>>> type(y)
<class 'list_iterator'>

#   iterator n. 迭代器

        —        next()逐个将迭代器中的元素提取出

>>> x = [1, 2, 3, 4, 5]
>>> y = iter(x)
>>> next(y)
1
>>> next(y)
2
>>> next(y)
3
>>> next(y)
4
>>> next(y)
5
>>> next(y)
Traceback (most recent call last):
  File "<pyshell#137>", line 1, in <module>
    next(y)
StopIteration

"""异常。异常是可控的,而错误则是不可控的,异常往往是程序员故意留下的"""

"""解决方法:"""

>>> x = [1, 2, 3, 4, 5]
>>> y = iter(x)
>>> next(y, "没啦,都被你掏空了~")
1
>>> next(y, "没啦,都被你掏空了~")
2
>>> next(y, "没啦,都被你掏空了~")
3
>>> next(y, "没啦,都被你掏空了~")
4
>>> next(y, "没啦,都被你掏空了~")
5
>>> next(y, "没啦,都被你掏空了~")
'没啦,都被你掏空了~'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值