整数,浮点数,字符串,对象
Python的列表可以装任意类型
append();extend();insert(,数据);
>>> number=["小甲鱼",123,3.14]
>>> number
['\xd0\xa1\xbc\xd7\xd3\xe3', 123, 3.14]
>>> len(number)
3
>>> number.append("黑夜")
>>> len(number)
4
>>> number
['\xd0\xa1\xbc\xd7\xd3\xe3', 123, 3.14, '\xba\xda\xd2\xb9']
>>> number.extend([2,3])
>>> number
['\xd0\xa1\xbc\xd7\xd3\xe3', 123, 3.14, '\xba\xda\xd2\xb9',2, 3]
>>> number.insert(0,"aaa")
>>> number
['aaa', '\xd0\xa1\xbc\xd7\xd3\xe3', 123, 3.14,'\xba\xda\xd2\xb9', 2, 3]
>>> number.insert(2,"c")
>>> number
['aaa', '\xd0\xa1\xbc\xd7\xd3\xe3', 'c', 123, 3.14,'\xba\xda\xd2\xb9', 2, 3]
第11课时列表:一个打了激素的数组2
交换数据位置
temp=number[0]
number[0]=number[1]
number[1]=temp
删除数据
number.remove();
del number[1]; number.pop(1)
>>> number=["牡丹","百合","菊花","绿竹"]
>>> number
['\xc4\xb5\xb5\xa4','\xb0\xd9\xba\xcf','\xbe\xd5\xbb\xa8','\xc2\xcc\xd6\xf1']
>>>number.remove("牡丹")
>>> print(number)
['\xb0\xd9\xba\xcf','\xbe\xd5\xbb\xa8', '\xc2\xcc\xd6\xf1']
>>>number=[1,2,3,4,5]
>>> number
[1, 2, 3, 4, 5]
>>> number.del(1)
SyntaxError: invalid syntax
>>> del.number[1]
SyntaxError: invalid syntax
>>> del number[1]
>>> number
[1, 3, 4, 5]
>>> number.pop()结尾的那个数字
5
>>> number
[1, 3, 4]
>>> number.pop(1) 列表的第二个数字
3
列表分片
>>>number=[1,2,3,4,5]
>>> number[:]
[1, 2, 3, 4, 5]
>>> number[1:3]
[2, 3]
>>> number[:3]
[1, 2, 3]
>>> number[2:]
[3, 4, 5]
>>>list1=[123]
>>>list2=[234]
>>>list1>list2
False
>>>list1=[123,234]
>>>list2=[234,123]
>>>list1>list2 从第一个元素开始比较起
False
>>>list3=[123,234]
>>>(list1>list2)and(list1==list3)
False
>>>list4=list1+list2
>>>list4
[123,234, 234, 123]
>>>list3*=3
>>>list3
[123,234, 123, 234, 123, 234]
>>>"小甲鱼" not in list3
True
>>>list5=[1,2,[3,4],5]
>>>3 in list5
False
>>>3 in list5[2]
True
>>>list5[2][0]
3
dir(list)
['__add__','__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__','__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__','__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__','__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__','__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__','__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__','__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop','remove', 'reverse', 'sort']
>>>list6=[123,456]
>>>list6*=5
>>>list6
[123,456, 123, 456, 123, 456, 123, 456, 123, 456]
>>>list6.count(123)
5
>>>list6.index(123)
0
>>>list6
[123,456, 123, 456, 123, 456, 123, 456, 123, 456]
>>>list6.index(123,3,7)
4
>>>list6.reverse()
>>>list6
[456,123, 456, 123, 456, 123, 456, 123, 456, 123]
>>>list7=[4,2,5,7,0,3,4,6]
>>> list7.sort()
>>>list7
[0,2, 3, 4, 4, 5, 6, 7]
>>>list7.sort(reverse=True)
>>>list7
[7,6, 5, 4, 4, 3, 2, 0]