|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 长路漫漫唯C作伴 于 2019-7-10 19:42 编辑
Python列表方法应用详解 方法 | 含义 | 返回值 | append()
| 将对象追加到末端
| L.append(object) ->None
| clear()
| 从列表中移除所有项目
| L.clear() -> None
| copy()
| 列表的浅拷贝
| L.copy() -> list
| count()
| 值出现的次数
| L.count(value) -> integer
| extend()
| 通过追加可迭代元素来扩展列表
| L.extend(iterable) -> None
| index()
| 返回第一个值的引索值,
如果值不存在,则引发ValueError
| L.index(value, [start, [stop]]) -> integer
| insert()
| 添加对象在索引值之前
| L.insert(index, object) -> None
| pop()
| 移除并返回索引项(默认为最后一个),如果List为空或索引超出范围,则引发IndexError
| L.pop([index]) -> item
| remove()
| 删除第一次出现的值,如果值不存在,则引发ValueError
| L.remove(value)
| reverse()
| 使列表翻转
| L.reverse() -> None
| sort()
| 稳定排序
| L.sort(key=None, reverse=False) -> None
|
代码示例:
- >>> list1=[1,"2",3.0]
- >>> list1.append(0)
- >>> list1
- [1, '2', 3.0, 0]
- >>> list1.extend([1,2,3])
- >>> list1
- [1, '2', 3.0, 0, 1, 2, 3]
- >>> list1.index(1)
- 0
- >>> list1.index(6)
- Traceback (most recent call last):
- File "<pyshell#72>", line 1, in <module>
- list1.index(6)
- ValueError: 6 is not in list
- >>> list1.insert(3,3.5)
- >>> list1
- [1, '2', 3.0, 3.5, 0, 1, 2, 3]
- >>> list1.pop(1)
- '2'
- >>> list1
- [1, 3.0, 3.5, 0, 1, 2, 3]
- >>> list1.remove(1)
- >>> list1
- [3.0, 3.5, 0, 1, 2, 3]
- >>> list1.remove(6)
- Traceback (most recent call last):
- File "<pyshell#79>", line 1, in <module>
- list1.remove(6)
- ValueError: list.remove(x): x not in list
- >>> list1.reverse()
- >>> list1
- [3, 2, 1, 0, 3.5, 3.0]
- >>> list1.sort()
- >>> list1
- [0, 1, 2, 3, 3.0, 3.5]
- >>> list1.sort(reverse=True)
- >>> list1
- [3.5, 3, 3.0, 2, 1, 0]
- >>> list2=list1.copy()
- >>> list2
- [3.5, 3, 3.0, 2, 1, 0]
- >>> list2.clear()
- >>> list2
- []
- >>> list1.count(3)
- 2
- >>> list1.count(2)
- 1
复制代码 |
|