With len View list length:
list addition, which is equivalent to connecting two lists sequentially:
The list is multiplied by integers, which is the equivalent of repeating the list:
Lists can be modified by index and Shard.
A = [ten, one, one, 14]a[0] = 100print a[100, 11, 12, 13, 14]
For contiguous shards (that is, the step size 1 ),Python takes the method of replacing the whole segment, and the number of elements does not need to be the same, for example, to be [11,12] replaced by [1,2,3,4] :
A = [ten, one, one, 14]a[1:3] = [1, 2, 3, 4]print a[10, 1, 2, 3, 4, 13, 14]
When you modify a fragment that is not contiguous (interval step is not 1), the number of elements must be the same:
A = [ten, one, one, 14]a[::2] = [1, 2, 3]a[1, 11, 2, 13, 3]
Python provides a way to remove elements from the list ' del '.
Delete the elements of the interval:
A = [' A ', 1, ' B ', 2, ' C ']del a[::2]a[1, 2]
into see if an element is in a sequence (not just a list), use not in to determine if it is not within a sequence.
A = [ten, one, one, ten, 14]print in Aprint, atruefalse# can also be used for strings: s = ' Hello world ' print ' he ' in sprint ' world ' no T in Struefalse
The number of elements in the list an element in the Count list index¶ to the list by adding a single element 1.append(ob)The elementobAdd to ListlThe last. Add a sequence to a list
.extend(lst)Adds the lst elements of the sequence to the end of the list, in turn, to the l equivalent l += lst .
inserting elementsinsert (idx, ob)In the indexidxInserted atob, and then the elements are moved back in turn. remove elementremove (ob)Will be the first one in the list that appearsobdeleted, ifobNotlError popup element inpop(idx)will be indexedidxRemoves the element and returns the element. Sortsort()Sorts the elements in the list by a certain rule if you do not want to change the values in the original list, you can use thesortedFunction:
A = [ten, 1, one, one, one, 2]a.sort () print a[1, 2, ten, one, one, 13]a = [1, one, one, one, 2]b = sorted (a) print aprint b[10, 1 , 11, 13, 11, 2][1, 2, 10, 11, 11, 13]
List Reversereverse()The elements in the list are arranged from the back forward.
If you do not want to change the values in the original list, you can use this method:
A = [1, 2, 3, 4, 5, 6]b = A[::-1]print aprint b[1, 2, 3, 4, 5, 6][6, 5, 4, 3, 2, 1]
Python----------List