python3列表方法统计,,1、count()官
python3列表方法统计,,1、count()官
1、count()
官方说明:def count(self, value): # real signature unknown; restored from __doc__ """ L.count(value) -> integer -- return number of occurrences of value """ return 0View Code
示例1:描述:统计列表中指定值的位置
参数:value 指定的值
返回值:返回这个值在列表中的位置,若未找到则返回0
l = [‘knight‘,‘jingliyang‘,‘egon‘,‘yuanhao‘]l1 = l.count(‘jingliyang‘)print(type(l1),l1)
输出结果:
<class ‘int‘> 1View Code示例2:
l = [‘knight‘,‘jingliyang‘,‘egon‘,‘yuanhao‘]l1 = l.count(‘kkkk‘) # 若没有找到则返回0print(type(l1),l1)
输出结果:
<class ‘int‘> 0View Code
2、index()
官方说明:def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0View Code
示例1:描述:与count()方法相似,统计列表中指定值的位置,不同之处是没找到指定的值则会抛出异常
参数:value 指定的值
start 起始位置
stop 结束位置
返回值:返回这个值在列表中的位置,若未找到则抛出异常
l = [‘knight‘,‘jingliyang‘,‘egon‘,‘yuanhao‘]l1 = l.index(‘jingliyang‘)print(type(l1),l1)
输出结果:
<class ‘int‘> 1View Code示例2:
l = [‘knight‘,‘jingliyang‘,‘egon‘,‘yuanhao‘]l1 = l.index(‘kkkkk‘)print(type(l1),l1)
输出结果:
Traceback (most recent call last): File "C:/Users/William/PycharmProjects/Knight/练习区/day3/练习1.py", line 2, in <module> l1 = l.index(‘kkkkk‘)ValueError: ‘kkkkk‘ is not in listView Code
3、append()
官方说明:
def append(self, p_object): # real signature unknown; restored from __doc__ """ L.append(object) -> None -- append object to end """ passView Code
示例:描述:在列表的末尾添加元素
参数:p_object 添加的元素
返回值:None(原列表会被修改)
l = [‘william‘,‘lisa‘,‘knight‘,‘pudding‘]l.append(‘sky‘)print(type(l),l)
输出结果:
<class ‘list‘> [‘william‘, ‘lisa‘, ‘knight‘, ‘pudding‘, ‘sky‘]View Code
4、insert()
官方说明:
def insert(self, index, p_object): # real signature unknown; restored from __doc__ """ L.insert(index, object) -- insert object before index """ passView Code
示例:描述:将元素插入到列表中指定的位置
参数:index 指定的索引位置
p_object 要插入的元素
返回值:None(原列表会被修改)
l = [‘william‘,‘lisa‘,‘knight‘,‘pudding‘]l.insert(2,‘sky‘) # 往索引2的位置前面插入一个“sky”的元素print(type(l),l)
输出结果:
<class ‘list‘> [‘william‘, ‘lisa‘, ‘sky‘, ‘knight‘, ‘pudding‘]View Code
5、extend()
官方说明:
def extend(self, iterable): # real signature unknown; restored from __doc__ """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """ passView Code
示例:描述:扩展列表(扩展的值添加到列表末尾)
参数:iterable 元素列表
返回值:None(原列表会被修改)
l = [‘william‘,‘lisa‘,‘knight‘,‘pudding‘]l2 = [‘hello‘,‘world‘]l.extend(l2) # 为列表“l”扩展print(type(l),l)
输出结果:
<class ‘list‘> [‘william‘, ‘lisa‘, ‘knight‘, ‘pudding‘, ‘hello‘, ‘world‘]View Code
6、copy()
官方说明:
def copy(self): # real signature unknown; restored from __doc__ """ L.copy() -> list -- a shallow copy of L """ return []View Code
示例:描述:复制列表
参数:无
返回值:得到一个复制后的新列表
l = [‘william‘,‘lisa‘,‘knight‘,‘pudding‘]l2 = l.copy()print(type(l),l)print(type(l2),l2)
输出结果:
<class ‘list‘> [‘william‘, ‘lisa‘, ‘knight‘, ‘pudding‘]<class ‘list‘> [‘william‘, ‘lisa‘, ‘knight‘, ‘pudding‘]View Code
7、clear()
官方说明:
def clear(self): # real signature unknown; restored from __doc__ """ L.clear() -> None -- remove all items from L """ passView Code
示例:描述:清空列表中所有的元素
参数:无
返回值:无(原列表会被修改)
l = [‘william‘,‘lisa‘,‘knight‘,‘pudding‘]l.clear()print(type(l),l)
输出结果:
<class ‘list‘> []View Code
8、pop()
官方说明:
def pop(self, index=None): # real signature unknown; restored from __doc__ """ L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. """ passView Code
示例1描述:移除列表中指定的元素(默认删除列表中最后一个元素)
参数:index 索引位置
返回值:返回被移除的元素
l = [‘william‘,‘lisa‘,‘knight‘,‘pudding‘]l1 = l.pop() # 不指定索引位置,默认删除列表中最后一个元素print(type(l),l)
输出结果:
<class ‘list‘> [‘william‘, ‘lisa‘, ‘knight‘]View Code示例2:
l = [‘william‘,‘lisa‘,‘knight‘,‘pudding‘]l.pop(2) # 指定删除索引2位置的元素print(type(l),l)
输出结果:
<class ‘list‘> [‘william‘, ‘lisa‘, ‘pudding‘]View Code
9、sort()
官方说明:
def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__ """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """ passView Code
描述:对列表进行排序
参数:key 默认key=None,key在使用时必须提供一个排序过程总调用的函数
reverse 默认reverse=False,当reverse=True时元素的排序会按降序排序
返回值:None(原列表会被修改)
示例1:l = [‘william‘,‘lisa‘,‘knight‘,‘pudding‘]l.sort() # 默认按元素中的第一个字母,以a-z的顺序进行排序print(type(l),l)
输出结果:
<class ‘list‘> [‘knight‘, ‘lisa‘, ‘pudding‘, ‘william‘]View Code示例2:
l = [5,3,2,7,4,1,6]l.sort() # 数字按0-9的升序排序。注意列表中若既有数字又有字符串则不能用sort方法排序,否则报错print(type(l),l)
输出结果:
<class ‘list‘> [1, 2, 3, 4, 5, 6, 7]View Code示例3:
l = [5,3,2,7,4,1,6]l.sort() # 不加参数时默认按升序排序print(type(l),l)l.sort(reverse=True) # 加上reverse=True时,表示将列表按降序排序print(type(l),l)
输出结果:
<class ‘list‘> [1, 2, 3, 4, 5, 6, 7]<class ‘list‘> [7, 6, 5, 4, 3, 2, 1]View Code示例4
l = [‘x‘,‘xxxx‘,‘xx‘,‘xxxxx‘,‘xxx‘]l.sort(key=len) # 提供一个排序过程调用的函数,本例用len函数得到元素的长度,然后按升序排序print(type(l),l)
输出结果:
<class ‘list‘> [‘x‘, ‘xx‘, ‘xxx‘, ‘xxxx‘, ‘xxxxx‘]View Code
10、reverse()
官方说明:
def reverse(self): # real signature unknown; restored from __doc__ """ L.reverse() -- reverse *IN PLACE* """ passView Code
描述:反转列表中的元素
参数:无
返回值:None (原列表会被修改)
示例:l = [‘william‘,‘lisa‘,‘knight‘,‘pudding‘]l.reverse()print(type(l),l)
输出结果:
<class ‘list‘> [‘pudding‘, ‘knight‘, ‘lisa‘, ‘william‘]View Code
11、remove()
官方说明:
def remove(self, value): # real signature unknown; restored from __doc__ """ L.remove(value) -> None -- remove first occurrence of value. Raises ValueError if the value is not present. """ passView Code
描述:移除列表中指定的元素
参数:value 指定的元素
返回值:None(原列表会被修改)
示例:l = [‘william‘,‘lisa‘,‘knight‘,‘pudding‘]l.remove(‘lisa‘)print(type(l),l)
输出结果:
<class ‘list‘> [‘william‘, ‘knight‘, ‘pudding‘]View Code
python3列表方法统计
相关内容
- 如何在Ubuntu 18.04上安装Python 3.8,,Python是世界上
- 彻底搞懂Python 中的 import 与 from import,,对不少 Python
- Python姹備竴鍏冧簩娆℃柟绋嬭В,,鏍囩锛?a hre
- 自动化测试框架、Python面向对象以及POM设计模型简介,
- Python input() 函数,,Python3.x
- python中的类和对象,属性和方法,,一、面向对象的概述
- python3中的真值测试,,1. 真值测试所谓真
- python学习之描述符,,class Foo:
- Python的类和对象 入门级讲解(简单粗暴),,前言:本
- python 删除文件夹,,只能删除空文件夹,删
评论关闭