list中所有方法的使用

简介: 学习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']


相关文章
|
2月前
|
Java 开发者
使用HashMap的values()方法返回的值转换为List时遇到错误
使用HashMap的values()方法返回的值转换为List时遇到错误
|
2月前
|
存储 缓存 NoSQL
利用Redis List实现数据库分页快速查询的有效方法
利用Redis List实现数据库分页快速查询的有效方法
|
7月前
|
Java
Java 清空 List 的多种方法?
Java 清空 List 的多种方法?
569 0
|
2天前
|
Java API
【亮剑】Java的List,如何删除重复的元素,教你三个方法搞定!
【4月更文挑战第30天】本文介绍了三种Java中删除List重复元素的方法:1) 使用HashSet,借助其不允许重复值的特性;2) 利用Java 8 Stream API的distinct()方法;3) 对自定义对象重写equals()和hashCode()。每种方法都附带了代码示例,帮助理解和应用。
|
1月前
List中的remove方法遇到报错不能删除以及四种解决办法点赞收藏
List中的remove方法遇到报错不能删除以及四种解决办法点赞收藏
16 0
|
2月前
使用List中的remove方法遇到数组越界
使用List中的remove方法遇到数组越界
17 2
|
2月前
|
Java
JAVA——List中剔除空元素(null)的三种方法汇总
JAVA——List中剔除空元素(null)的三种方法汇总
|
3月前
|
存储 Java 索引
java list集合相关介绍和方法使用操作
java list集合相关介绍和方法使用操作
30 1
|
5月前
|
索引
看一下List接口的常用方法
看一下List接口的常用方法
36 1
|
5月前
|
Python
python中ndarray与list转换的方法
python中ndarray与list转换的方法
31 0