|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
012列表:一个打了激素的数组III
一、列表的常用操作符:比较、逻辑、连接、重复、成员关系
1、比较操作符
- >>> list1 = [123,456]
- >>> list2 = [234,123]
- >>> list1 > list2
- False
复制代码
2、逻辑操作符
- >>> list3 = [123,456]
- >>> (list1 < list2) and (list1 == list3)
- True
复制代码
3、连接操作符
- >>> list4 = list1 + list2
- >>> list4
- [123, 456, 234, 123]
复制代码
4、重复操作符
- >>> list3 * 3
- [123, 456, 123, 456, 123, 456]
复制代码
5、成员关系操作符
- >>> list5 = [123,['小甲鱼','牡丹'],456]
- >>> '小甲鱼' in list5
- False
- >>> '小甲鱼' in list5[1]
- True
复制代码
二、列表类型的内置函数BIF:s.BIF()
Count、index、reverse、sort
- >>> dir(list)
- ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
复制代码
1、count:计算参数在列表中出现的次数
- >>> list3 = [123,456]
- >>> list3.count(123)
- 1
复制代码
2、index:索引,参数在列表中的第一个位置
- >>> list3
- [123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456]
- >>> list3.index(123)
- 0
- >>> list3.index(123,3,7)
- 4
复制代码
3、reverse:倒排序
- >>> list5 = [1,2,3]
- >>> list5.reverse()
- >>> list5
- [3, 2, 1]
复制代码
4、sort:排序
- >>> list6 = [5,8,3,5]
- >>> list6.sort()
- >>> list6
- [3, 5, 5, 8]
- >>> list6.sort(reverse=True)
- >>> list6
- [8, 5, 5, 3]
复制代码
三、分片“拷贝”的补充:list6[:]与list6的区别
- >>> list7 = list6[:]
- >>> list7
- [8, 5, 5, 3]
- >>> list8 = list6
- >>> list8
- [8, 5, 5, 3]
- >>> list6.sort()
- >>> list6
- [3, 5, 5, 8]
- >>> list7
- [8, 5, 5, 3]
- >>> list8
- [3, 5, 5, 8]
复制代码
|
|