Python列表操作方法的整理

492次阅读
没有评论

Python列表操作方法的整理

1、append用于在列表末尾追加新的对象

a = [1,2,3]
a.append(4)                          #the result : [1, 2, 3, 4]

2、count方法统计某个元素在列表中出现的次数

a = ['aa','bb','cc','aa','aa']
print(a.count('aa'))                 #the result : 3

3、extend方法可以在列表的末尾一次性追加另一个序列中的多个值

a = [1,2,3]
b = [4,5,6]
a.extend(b)                          #the result :[1, 2, 3, 4, 5, 6]

index函数用于从列表中找出某个值第一个匹配项的索引位置

a = [1,2,3,1]
print(a.index(1))                   #the result : 0

insert方法用于将对象插入到列表中

a = [1,2,3]
a.insert(0,'aa')            #the result : ['aa', 1, 2, 3]

pop方法会移除列表中的一个元素(默认是最后一个),并且返回该元素的值

a = [1,2,3]
a.pop()                             #the result : [1, 2]
a.pop(0)
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2022-05-26发表,共计614字。
新手QQ群:570568346,欢迎进群讨论 Python51学习