python的list的用法其实有很多,这里就练习几个常用的吧。
这里是S的几个函数
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 as s[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 as s[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 |