提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
list中所有方法的使用
前言
学习List(列表)的使用方法。
/之前不能使用 值=什么 *之后只能使用 值=什么
一、append()
append(self, object, /)
Append object to the end of the list. #将对象追加到列表的末尾。
样例:
list_var = [1, 2, 3]
print(list_var)
list_var.append(4)
print(list_var)
[1, 2, 3]
[1, 2, 3, 4]
二、clear()
clear(self, /)
Remove all items from list. #从列表中删除所有项目
样例:
list_var = [1, 2, 3]
print(list_var)
list_var.clear()
print(list_var)
[1, 2, 3]
[]
三、copy()
这一个拉出来单说点这里 链接
四、count()
count(self, value, /)
Return number of occurrences of value. #返回值的出现次数。
样例:
list_var = [1, 2, 3, 3]
return_value = list_var.count(3)
print(return_value, type(return_value))
#3出现了两次其输出结果为2
2 <class 'int'>
五、extend
Extend list by appending elements from the iterable.
#通过从可迭代对象追加元素来扩展列表,
#iterable(迭代)--序列(tuple,str,byte,list)
样例:
list_var = [1, 2, 3]
print(list_var)
list_var.extend((4, 5, 6))
print(list_var)
[1, 2, 3]
[1, 2, 3, 4, 5, 6]
1.1 extend和append的区别
extend 是把每个序列里面的每一个元素添加到列表结尾
append 是把一个整体添加到列表结尾
样例:
list_var = [1, 2, 3]
list_var.extend(tuple('456'))
print(list_var)
list_var = [1, 2, 3]
list_var.append(tuple('456'))
print(list_var)
[1, 2, 3, '4', '5', '6']
[1, 2, 3, ('4', '5', '6')]
六、index
index(self, value, start=0, stop=9223372036854775807, /)
#Return first index of value.----返回值的第一个索引
#Raises ValueError if the value is not present.----如果值不存在,则提高值错误。
样例:
list_var = [4, 2, 3, 9, 5, 6, 0]
return_value = list_var.index(3)
print(return_value)
这里3的下标是 return_value的值是2
2
七、insert
insert(self, index, object, /)
#Insert object before index.----在索引之前插入对象
#在指定位置插入值
样例:
list_var = [1, 2, 4]
list_var.insert(2, 3)
print(list_var)
在下标是2的位置插入元素3
[1, 2, 3, 4]
八、pop
Remove and return item at index (default last).
#删除并返回索引处的项目(默认最后)
Raises IndexError if list is empty or index is out of range.
#如果列表为空或索引超出范围,则引发索引错误。
样例:
list_var = [1, 2, 3]
return_value = list_var.pop(1)
print(return_value)
print(list_var)
删除下标为1的值
[1, 3]
九、remove
Remove first occurrence of value.
#删除第一个现的值。
Raises ValueError if the value is not present.
#提高值如果值不存在则错误
样例:
list_var = [1, 1, 2, 3]
return_value = list_var.remove(1)
print(return_value)
print(list_var)
删除出现的第一个值‘1’
None
[1, 2, 3]
十、reverse
reverse(self, /)
Reverse *IN PLACE*.#倒置输出
样例:
list_var = [1, 3, 2]
list_var.reverse()
print(list_var)
1,3,2 倒置 2,3,1
list_var = [1, 3, 2]
list_var.reverse()
print(list_var)
[2, 3, 1]
十一、 sort
sort(self, /, *, key=None, reverse=False)
#排序之后----key是指定排序的规则
Sort the list in ascending order and return None.
#按升序对列表进行排序,并返回 None
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).
#排序是就地的(即列表本身被修改)和稳定的(即保持两个相等元素的顺序)
f a key function is given, apply it once to each list item and sort them,ascending or descending, according to their function values.
#如果给出了关键函数,则将其应用于每个列表项一次,然后根据其函数值对它们进行排序,升序或降序
样例:默认规则
list_var = [1, 3, 5, 2, 6, 8, 4, 9]
list_var.sort()
print(list_var)
[1, 2, 3, 4, 5, 6, 8, 9]
样例:自定义规则
根据尾字母按照ASCII排序,如果最后一个尾字母相同则按照倒数第二个,如果倒数第二个尾字母相同就按照倒数第三个,倒数第三个相同,就按照之前定义的位置输出
def get_last_char(str_data):
return str_data[-1], str_data[-2], str_data[-3]
fruit_list = ["banana", "durian", "pineapple", "cherry", "strawberry", "kiwifruit", "peach", "grape"]
fruit_list.sort(key=get_last_char)
print(fruit_list)
['banana', 'pineapple', 'grape', 'peach', 'durian', 'kiwifruit', 'cherry', 'strawberry']