Python List, pythonlist
First, emphasize:
LIST has very powerful functions in python.
Definition
Simple definition: in python, square brackets are used to represent a list, []
Features
I. It can be infinitely large, that is, the elements that can be accommodated in the listQuantityUnlimited
2. The elements in the list can be of any type, int, str, or even list, or even the dict to be learned in the future. Arrays and lists in java are similar, but the elements in the array can only be one type.
3. Important features: the list can be modified. This modification does not copy a new one, but is modified in the original place.
Use
Index and slice usage
In [17]: a = [1, '2', 'Hello world'] In [18]: a [0] # Slice Index Out [18]: 1In [19]: a [:] Out [19]: [1, '2', 'Hello world'] In [20]: [: 2] Out [20]: [1, '2'] In [21]:. index (1) Out [21]: 0In [22]: a [:-1] # reverse an Out [22]: ['Hello world', '2 ', 1] In [24]: list (reversed (a) Out [24]: ['Hello world', '2', 1]
Basic operation usage
Len () + * in max () and min () cmp () append ()
You can use dir (list) to view all the usage methods of 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']
Differences between append and extend
We recommend that you directly run the code to view it:
In [25]: a,b,la,lb = [1,2,3],[3,4,5],[1,2,3],[3,4,5]In [26]: aOut[26]: [1, 2, 3]In [27]: bOut[27]: [3, 4, 5]In [28]: laOut[28]: [1, 2, 3]In [29]: lbOut[29]: [3, 4, 5]In [31]: a.append(b)In [32]: la.extend(lb)In [33]: aOut[33]: [1, 2, 3, [3, 4, 5]]In [34]: laOut[34]: [1, 2, 3, 3, 4, 5]
It can be seen that extend is equivalent to list [len (list):] = L