马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.列表的一些常用操作符
1)比较操作符
- >>> list1 = [234]
- >>> list2 = [345]
- >>> list1 > list2
- False
- >>> 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]
- >>> list1 + '小甲鱼'
- Traceback (most recent call last):
- File "<pyshell#11>", line 1, in <module>
- list1 + '小甲鱼'
- TypeError: can only concatenate list (not "str") to list
复制代码
4)重复操作符
- >>> list3
- [123, 456]
- >>> list3 * 3
- [123, 456, 123, 456, 123, 456]
- >>> list3 *= 3
- >>> list3
- [123, 456, 123, 456, 123, 456]
- >>> list3 *= 5
- >>> 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]
复制代码
5)成员关系操作符
- >>> 123 in list3
- True
- >>> '小甲鱼' not in list3
- True
- >>> 123 not in list3
- False
- >>> list5 = [123,['小甲鱼','牡丹'],456]
- >>> '小甲鱼' in list5
- False
- >>> '小甲鱼' in list5[1]
- True
- >>> list5[1][1]
- '牡丹'
复制代码
2.列表的小伙伴们(官方的来讲:列表类型的内置函数BIF)
- >>> 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:计算它的参数在列表中出现的次数
2)index:索引,返回它的参数在列表中的位置
- >>> list3.index(123)
- 0
- >>> list3.index(123,3,7)
- 4
复制代码
3)reverse:将整个列表原地翻转
- >>> list3.reverse()
- >>> list3
- [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, 123]
复制代码
4)sort:用指定的方式对列表的成员进行排序
- >>> list6 = [4,2,5,1,9,23,32,0]
- >>> list6.sort() #从小到大排
- >>> list6
- [0, 1, 2, 4, 5, 9, 23, 32]
- >>> list6.sort(reverse=True) #从大到小排
- >>> list6
- [32, 23, 9, 5, 4, 2, 1, 0]
复制代码
3.关于分片‘拷贝’概念的补充
- >>> list7 = list6[:]
- >>> list7
- [32, 23, 9, 5, 4, 2, 1, 0]
- >>> list8 = list6
- >>> list8
- [32, 23, 9, 5, 4, 2, 1, 0]
- >>> list6.sort()
- >>> list6
- [0, 1, 2, 4, 5, 9, 23, 32]
- >>> list7
- [32, 23, 9, 5, 4, 2, 1, 0]
- >>> list8
- [0, 1, 2, 4, 5, 9, 23, 32]
复制代码 |