马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
第012讲 列表:一个打了激素的数组3
① 列表的一些常用操作符:
比较操作符、逻辑操作符、链接操作符、重复操作符、成员关系操作符
②
>>> list1 = [123]
>>> list2 = [234]
>>> list1>list2
False
>>> list1 = [123,456]
>>> list2 = [234,123]
>>> list1>list2
False
>>> list3 = [123,456]
>>> (list1<list2) and (list1 == list3)
True
>>> list4 = list1 + list2
>>> list4
[123, 456, 234, 123]
>>> list1 +'567'
Traceback (most recent call last):
File "<pyshell#82>", line 1, in <module>
list1 +'567'
>>> list1.append(678)
>>> list1
[123, 456, 678]
>>> '小甲鱼'not in list3
True
>>> list5 = [123,['小甲鱼','牡丹'],456]
>>> '小甲鱼'in list5[1]
True
③ 列表的小伙伴:
['__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']
如count表示出现次数
>>> list2 = [123,456,789]
>>> list2.insert(0,11)
>>> list3 = list2*6
>>> list3
[11, 123, 456, 789, 11, 123, 456, 789, 11, 123, 456, 789, 11, 123, 456, 789, 11, 123, 456, 789, 11, 123, 456, 789]
>>> list3.count(123)
6
index表示位置
>>> list2.index(123)
1
>>> list2.index(789)
3
reverse 把前后数字交换位置
>>> list3.reverse()
>>> list3
[789, 456, 123, 11, 789, 456, 123, 11, 789, 456, 123, 11, 789, 456, 123, 11, 789, 456, 123, 11, 789, 456, 123, 11]
sort表示排序
>>> list6 = [4,2,1,3,7,10]
>>> list6.sort()
>>> list6
[1, 2, 3, 4, 7, 10]
有关拷贝 (python3)
>>> list6.sort(reverse = True)
>>> list6
[10, 7, 4, 3, 2, 1]
>>> list7 = list6[:]
>>> list7
[10, 7, 4, 3, 2, 1]
>>> list8 = list6
>>> list8
[10, 7, 4, 3, 2, 1]
>>> list6.sort()
>>> list6
[1, 2, 3, 4, 7, 10]
>>> list7
[10, 7, 4, 3, 2, 1]
>>> list8
[1, 2, 3, 4, 7, 10]
list6 --->[1,2,3,4,7,10]<--- list8
|
list7 --->[1,2,3,4,7,10] |