本文共 2416 字,大约阅读时间需要 8 分钟。
Operation | Result | Notes |
---|---|---|
s[i] = x | item i of s is replaced by x | |
s[i:j] = t | slice of s from i to j is replaced by the contents of the iterable t | |
del s[i:j] | same as s[i:j] = [] | |
s[i:j:k] = t | the elements of s[i:j:k] are replaced by those of t | (1) |
del s[i:j:k] | removes the elements of s[i:j:k] from the list | |
s.append(x) | appends x to the end of the sequence (same ass[len(s):len(s)] = [x]) | |
s.clear() | removes all items from s (same as del s[:]) | (5) |
s.copy() | creates a shallow copy of s (same as s[:]) | (5) |
s.extend(t) | extends s with the contents of t (same ass[len(s):len(s)] = t) | |
s.insert(i, x) | inserts x into s at the index given by i (same as s[i:i] =[x]) | |
s.pop([i]) | retrieves the item at i and also removes it from s | (2) |
s.remove(x) | remove the first item from s where s[i] == x | (3) |
s.reverse() | reverses the items of s in place | (4) |
Notes:
t must have the same length as the slice it is replacing.
The optional argument i defaults to -1, so that by default the last item is removed and returned.
remove raises when x is not found in s.
The reverse() method modifies the sequence in place for economy of space when reversing a large sequence. To remind users that it operates by side effect, it does not return the reversed sequence.
clear() and copy() are included for consistency with the interfaces of mutable containers that don’t support slicing operations (such as and)
New in version 3.3: clear() and copy() methods.
转载地址:http://fstta.baihongyu.com/