List
Conversion between the string and list
>>> li = list('hello')
>>> li
['h', 'e', 'l', 'l', 'o']
>>> li[0] = 'H'
>>> li
['H', 'e', 'l', 'l', 'o']
>>> ''.join(li)
'Hello'
>>>
List Removing elements
>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>
>>> del numbers[0:3]
>>> numbers
[4, 5, 6, 7, 8, 9, 10]
>>>
To slice assignments
Slice assignment to a plurality of elements can be simultaneously assigned may be replaced slices length to its different sequences.
>>> name = list('Perl')
>>> name[1:] = 'ython'
>>> name
['P', 'y', 't', 'h', 'o', 'n']
Use slice assignments can also insert new elements without replacing the original elements.
>>> numbers = [1, 5]
>>> numbers[1:1]
[]
>>> numbers[1:1] = [2, 3, 4]
>>> numbers
[1, 2, 3, 4, 5]
append
Attaching an object to the end of the list.
>>> numbers
[11, 10, 9, 8, 7]
>>> numbers.append(1)
>>> numbers
[11, 10, 9, 8, 7, 1]
clear
Empty the contents of the list.
>>> numbers
[11, 10, 9, 8, 7, 1]
>>> numbers.clear()
>>> numbers
[]
copy
Copy the list, just copy the routine associated with another name to the list.
>>> a = [1, 2, 3]
>>> b = a
>>> b
[1, 2, 3]
>>> a[1] = 4
>>> a
[1, 4, 3]
>>> b
[1, 4, 3]
Let a and b point to a different list, you must make a copy of the association b.
>>> a = [1, 2, 3]
>>> b = a.copy()
>>> b
[1, 2, 3]
>>> b[1] = 4
>>> b
[1, 4, 3]
>>> a
[1, 2, 3]
count
Calculate the number of specified elements appear in the list.
>>> ['to', 'be', 'or', 'not', 'to', 'be'].count('to')
2
extend
A plurality of values may be appended to the end of the list, the sequence of these values may be provided to extend the method as a parameter.
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
>>>
This effect is similar to the stitching, the difference is, extend modifies the sequence is about to be expanded, and the stitching is to return a new sequence.
index
Finds the index of the first occurrence of a value in the list.
>>> knights = ['We', 'are', 'the', 'knights', 'who', 'say', 'ni']
>>> knights.index('who')
4
insert
Insert an object list.
>>> numbers = [1, 2, 3, 4, 5, 6]
>>> numbers
[1, 2, 3, 4, 5, 6]
>>> numbers.insert(3, 'four')
>>> numbers
[1, 2, 3, 'four', 4, 5, 6]
>>>
pop
Delete an element (default is the last element) from the list and return to the elements.
>>> numbers
[1, 2, 3, 'four', 4, 5, 6]
>>> numbers.pop()
6
>>> numbers.pop(-2)
4
remove
Remove the first element to the specified value
>>> x = ['a', 'b', 'a', 'c', 'd']
>>> x.remove('a')
>>> x
['b', 'a', 'c', 'd']
>>>
reverse
To reverse the order of elements in the list
>>> numbers = [1, 2, 3]
>>> numbers.reverse()
>>> numbers
[3, 2, 1]
sort
Sort the list in place, local sorting means that the original list to be modified so that their entries are arranged in order, instead of returning a copy of the list sorted.
>>> a = [5, 3, 4, 2, 1]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5]
sort takes two optional parameters: key and reverse, these two parameters are usually specified by name, called keyword parameters.
A sorting function is provided for using it to create a key for each element, the elements are sorted according to these keys.
>>> x = ['apple', 'tomato', 'banana', 'orange', 'pear']
>>> x.sort(key=len)
>>> x
['pear', 'apple', 'tomato', 'banana', 'orange']
>>>
reverse specify a true value (True, False) to forward or reverse sort the list.
>>> numbers = ['5', '4', '7', '9', '2']
>>> numbers.sort(reverse=True)
>>> numbers
['9', '7', '5', '4', '2']
>>> numbers.sort(reverse=False)
>>> numbers
['2', '4', '5', '7', '9']
Tuple
And lists, tuples is the sequence, the only difference is a tuple is not modifiable . Just some values separated by commas, you can automatically create a tuple.
It contains only one value tuple
>>> 42,
(42,)
Empty tuple
>>> ()
()
tuple
One sequence as a parameter, and is converted to a tuple.
>>> tuple([1, 2 , 3])
(1, 2, 3)
>>> tuple('python')
('p', 'y', 't', 'h', 'o', 'n')
>>>