|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 ~风介~ 于 2015-11-23 22:50 编辑
方法:- append(...)
- L.append(object) -> None -- append object to end
复制代码
代码:
- >>> myList = [1,2]
- >>> myList.append({'Fish':1})
- >>> myList
- [1, 2, {'Fish': 1}]
复制代码 ===
方法:
- clear(...)
- L.clear() -> None -- remove all items from L
复制代码
代码:
- >>> myList.clear()
- >>> myList
- []
- >>>
复制代码 ===
方法:
- copy(...)
- L.copy() -> list -- a shallow copy of L
复制代码
代码:
- >>> myList = [1, 2, {'Fish': 1}]
- >>> newList = myList.copy()
- >>> newList
- [1, 2, {'Fish': 1}]
- >>> del myList
- >>> newList
- [1, 2, {'Fish': 1}]
- >>>
复制代码 ===
方法:
- count(...)
- L.count(value) -> integer -- return number of occurrences of value
复制代码
代码:
- >>> myList = [1, 2, {'Fish': 1},1,1]
- >>> myList.count(1)
- 3
- >>> myList.count({'Fish': 1})
- 1
- >>>
复制代码 ===
方法:
- extend(...)
- L.extend(iterable) -> None -- extend list by appending elements from the iterable
复制代码
代码:
- >>> myList
- [1, 2, {'Fish': 1}, 1, 1]
- >>> myList.extend(['Good','Morning'])
- >>> myList
- [1, 2, {'Fish': 1}, 1, 1, 'Good', 'Morning']
- >>>
复制代码 ===
方法:
- index(...)
- L.index(value, [start, [stop]]) -> integer -- return first index of value.
- Raises ValueError if the value is not present.
复制代码
代码:
- >>> myList
- [1, 2, {'Fish': 1}, 1, 1, 'Good', 'Morning']
- >>> myList.index(1)
- 0
- >>> myList.index(1,1,5)
- 3
- >>> myList.index(1111)
- Traceback (most recent call last):
- File "<pyshell#51>", line 1, in <module>
- myList.index(1111)
- ValueError: 1111 is not in list
- >>>
复制代码 ===
方法:
- insert(...)
- L.insert(index, object) -- insert object before index
复制代码
代码:
- >>> myList
- [1, 2, {'Fish': 1}, 1, 1, 'Good', 'Morning']
- >>> myList.insert(0,'FishC')
- >>> myList
- ['FishC', 1, 2, {'Fish': 1}, 1, 1, 'Good', 'Morning']
- >>> myList.insert(-1,'FishC')
- >>> myList
- ['FishC', 1, 2, {'Fish': 1}, 1, 1, 'Good', 'FishC', 'Morning']
- >>>
复制代码 ===
方法:
- pop(...)
- L.pop([index]) -> item -- remove and return item at index (default last).
- Raises IndexError if list is empty or index is out of range.
复制代码
代码:
- >>> myList
- ['FishC', 1, 2, {'Fish': 1}, 1, 1, 'Good', 'FishC', 'Morning']
- >>> myList.pop()
- 'Morning'
- >>> myList.pop(0)
- 'FishC'
- >>> myList
- [1, 2, {'Fish': 1}, 1, 1, 'Good', 'FishC']
- >>>
复制代码 ===
方法:
- remove(...)
- L.remove(value) -> None -- remove first occurrence of value.
- Raises ValueError if the value is not present.
复制代码
代码:
- >>> myList
- [1, 2, {'Fish': 1}, 1, 1, 'Good', 'FishC']
- >>> myList.remove(1)
- >>> myList
- [2, {'Fish': 1}, 1, 1, 'Good', 'FishC']
- >>> myList.remove()
- Traceback (most recent call last):
- File "<pyshell#64>", line 1, in <module>
- myList.remove()
- TypeError: remove() takes exactly one argument (0 given)
- >>>
复制代码 ===
方法:
- reverse(...)
- L.reverse() -- reverse *IN PLACE*
复制代码
代码:
- >>> myList
- [2, {'Fish': 1}, 1, 1, 'Good', 'FishC']
- >>> myList.reverse()
- >>> myList
- ['FishC', 'Good', 1, 1, {'Fish': 1}, 2]
- >>>
复制代码 ===
方法:
- sort(...)
- L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
复制代码
代码:
- >>> myList = [1,2,1,2,3,5,9]
- >>> myList.sort()
- >>> myList
- [1, 1, 2, 2, 3, 5, 9]
- >>> myList = [1,2,1,2,3,5,9]
- >>> myList.sort(reverse=True)
- >>> myList
- [9, 5, 3, 2, 2, 1, 1]
- >>>
复制代码
|
|