In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the methods of Python list". In daily operation, I believe many people have doubts about what are the methods of Python list. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "what are the methods of Python list"! Next, please follow the small series to learn together!
print(dir(list()))#Ways to view lists [..., 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
1. append()方法
描述:append() 方法在列表ls最后(末尾)添加一个元素object
语法:ls.append(object) object为要添加的元素。
参数:object可以添加 列表,字典,元组,集合,字符串等。
#append()函数的操作对象是原列表。ls = [1,2,3,4,5,6] ls.append(12)#添加元素 print(ls) [1, 2, 3, 4, 5, 6, 12] ls.append([1,"a"]) #添加列表 print(ls) [1, 2, 3, 4, 5, 6, 12, [1, 'a']] ls.append({2:"a",3:"hj"}) #添加字典 print(ls) [1, 2, 3, 4, 5, 6, 12, [1, 'a'], {2: 'a', 3: 'hj'}] ls.append((1,"k",3)) #添加元组 print(ls) [1, 2, 3, 4, 5, 6, 12, [1, 'a'], {2: 'a', 3: 'hj'}, (1, 'k', 3)] ls.append({"1","2","h"}) #添加集合 print(ls) [1, 2, 3, 4, 5, 6, 12, [1, 'a'], {2: 'a', 3: 'hj'}, (1, 'k', 3), {'2', 'h', '1'}] ls.append("123abc") #添加字符串 print(ls) [1, 2, 3, 4, 5, 6, 12, [1, 'a'], {2: 'a', 3: 'hj'}, (1, 'k', 3), {'2', 'h', '1'}, '123ab
2. clear()方法
描述:删除列表ls中的所有元素。
语法: ls.clear()
ls = [1,2,3,"4",5,"a"] ls.clear() print(ls) []
3. copy()方法
描述:生成一个新列表,复制ls中的所有元素。
语法: ls.copy() -> list 返回一个列表
ls = [1,2,3,[4,5,6]] lt = ls.copy() #lt复制ls中的所有元素 ls.clear() #删除ls所有元素,lt中的元素没有被删除。 lk = ls #这不是复制,而是给列表ls新关联一个引用,即增加一个别名,ls和lt指向同一个内存地址。 print(id(ls),id(lk)) print(lt) print(ls)
4. count()方法
描述:统计列表ls中value元素出现的次数
语法:ls.count(value) -> integer 返回一个整数
参数:value--要统计的value元素。
ls = [1,2,3,5,4,5,5,5,5,"python"] ls.count(5) #统计列表ls中 5 出现的次数 5 ls.count(0)#列表ls中无0元素 0 ls.count("python") #统计列表ls中 "python" 出现的次数。
5. extend()方法
描述:在列表ls末尾添加一个列表iterable。
语法:ls.extend(iterable) -> None 无返回值
参数:iterable -- 要添加的列表。可以是整个列表iterable,也可以是列表iterable的一部分。
注意:extend() 和 append() 的不同之处在于:extend() 不会把列表或者元祖视为一个整体,而是把它们包含的元素逐个添加到列表中
ls = [1,2,"a",[4,5,"a"]] lt = [1,"abc","b",[1,2]] ls.extend(lt) #返回值为空,将列表lt的元素添加到列表ls末尾。 print(ls.extend(lt)) None print(ls) [1, 2, 'a', [4, 5, 'a'], 1, 'abc', 'b', [1, 2], 1, 'abc', 'b', [1, 2]] print(lt) #列表lt元素不变 [1, 'abc', 'b', [1, 2]]
6. index()方法
描述:列表ls中第一次出现元素value的位置。
语法: ls.index(value, start, stop) -> integer 返回一个整数
参数:
value -- 要查找的元素。
star -- 索引的起始位置。
stop -- 索引的结束位置。
ls = [1,2,3,"a",3,5,"a",5,[1,7,"b"]] ls.index("a") #返回列表ls中"a"第一次出现的位置。 2 ls.index("a",4) #索引的起始位置为下标为4的元素,索引范围为 3, 5, 'a', 5, [1, 7, 'b' 6 ls.index("a",4,8) #索引的起始位置为下标为4的元素,结束位置为下标为7的元素。索引范围 3, 5, 'a', 5 6
7. insert()方法
描述:在列表第index位置,添加元素object。
语法:ls.insert(index, object)
index -- 元素object插入列表ls的位置。
objece -- 将要添加的元素。可以是列表,元组,字典,集合,字符串等。
ls = [1,2,"a",["a",5,8]] ls.insert(3,"b")#在列表ls下标为3的位置插入元素 "b" print(ls) [1, 2, 'a', 'b', 'b', ['a', 5, 8]] ls.insert(3,1) #在列表ls下标为3的位置插入元素 1 print(ls) [1, 2, 'a', 1, 'b', 'b', ['a', 5, 8]] ls.insert(1,['a', 5, 8]) #在列表ls的内嵌套列表["a",5,8] print(ls) [1, ['a', 5, 8], 2, 'a', 1, 'b', 'b', ['a', 5, 8]] ls = [1,2,3] ls.insert(0,[1,2,3]) #插入列表 ls.insert(0,(1,2,3)) #插入元组 ls.insert(0,{1:"a",2:"b"}) #插入字典 ls.insert(0,{1,2,3}) #插入集合 print(ls) [{1, 2, 3}, {1: 'a', 2: 'b'}, (1, 2, 3), [1, 2, 3], 1, 2
8. pop()方法
描述:将列表ls中第index项元素取出,并从列表ls中删除该元素。若果省略index,则默认删除列表最后(末尾)一个元素,并返回该元素。
语法: ls.pop(index) -> item 返回删除的项
参数: index -- 要取出并删除的元素下标的序数。
ls = [1,2,"a","y",[1,2,3],"b"] ls.pop(0)#取出下标为0的元素,并从列表ls中删除。 1 print(ls) [2, 'a', 'y', [1, 2, 3], 'b'] ls.pop() #默认取出列表ls最后一个元素,并删除。 'b' print(ls) [2, 'a', 'y', [1, 2, 3]
9. remove()方法
描述:将列表ls中出现的第一个元素value删除。
语法:ls.remove(value) -> None 返回值为空
参数:value -- 要删除的元素。
ls1 = [1,2,"a",3,1,1,55,"a,1"] ls2 = [1,2,"a",3,1,1,55,"a,1"] ls1.remove(1) #删除ls1中第一次出现的元素 1 ls2.remove("a") ##删除ls2中第一次出现的元素 "a" print(ls1.remove(1)) #返回值为空 print(ls1) print(ls2)
10. reverse()方法
描述:将列表ls中的元素反转。
语法:ls.reverse()
ls1 = [1,2,3,4,5,6,7,8,9] ls1.reverse() #将列表ls1反转输出 print(ls1) [9, 8, 7, 6, 5, 4, 3, 2, 1] ls2 = [2,5,8,9,4,1,2,6,2,1,3] ls2.sort(reverse=True) #ls2.sort()默认将列表ls2按从小到大的序数排列。reverse=True 使排序后的列表反转,reverse=False 则不反转 print(ls2) [9, 8, 6, 5, 4, 3, 2, 2, 2, 1, 1]
11. sort() 方法
描述:将原列表ls中的元素进行排序,意味着改变原来的列表,而不是返回一个列表
语法:ls.sort([key=None][,reverse=False])--无返回值,但是会对列表中的元素进行排序。
参数:
key-- 可选参数, 如果指定了该参数会使用该参数的方法进行排序。
reverse-- 可选参数,是否反向排序,默认为False。
ls = [1,3,7,2,4,5,6] ls.sort() print(ls) [1, 2, 3, 4, 5, 6, 7]#原来的列表发生了改变 当用户需要一个排列好的列表,同时又要保留原来的列表时,怎么做 错误方法1: ls = [1,3,7,2,4,5,6] y = ls.sort() print(y) None print(ls) #错误方法2: ls = [1,3,7,2,4,5,6] y = ls y.sort() print(ls) [1, 2, 3, 4, 5, 6, 7] print(y) [1, 2, 3, 4, 5, 6, 7] 正确方法: ls = [1,3,7,2,4,5,6] y = ls[:] y.sort() print(ls) [1, 3, 7, 2, 4, 5, 6]#旧的列表未改变 print(y) [1, 2, 3, 4, 5, 6, 7]#新的列表改变 另外一种获取副本的方法,是用sorted() 函数 ls = [1,3,7,2,4,5,6] y = sorted(ls) print(y) [1, 2, 3, 4, 5, 6, 7] print(ls) [1, 3, 7, 2, 4, 5, 6]#未发生改变 降序的方法,现用sort 或者 sorted() 然后用reverse()进行反转 ls = [1,3,7,2,4,5,6] y = sorted(ls) y.reverse() print(y) [7, 6, 5, 4, 3, 2, 1] 当然,sort方法还有两个参数,key和reverse ls = ['a22112x','aaaba','xxvvv','5aa','wodesddddssd'] ls.sort(key=len) print(ls) ['5aa', 'aaaba', 'xxvvv', 'a22112x', 'wodesddddssd'] ##按a的个数进行排序 定义一个计算'a'的个数的函数 ls = ['1a22112x','2aaaba','3xxvvv','4b5aa','5wodesddddssd'] def a_fun(x): return str(x).count('a') ls.sort(key=a_fun) print(ls) ['3xxvvv', '5wodesddddssd', '1a22112x', '4b5aa', '2aaaba'] ls = [1,3,7,2,4,5,6] ls.sort(reverse=True) [7, 6, 5, 4, 3, 2, 1]到此,关于"Python列表的方法有哪些"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un