美文网首页
list mothod总结(python3.5)

list mothod总结(python3.5)

作者: Saber真是太可爱了 | 来源:发表于2015-11-27 13:37 被阅读0次

1. 创建一个空的list

>>>a = []
>>>a = list()

2. L.append(object)

append用于在list末尾追加一个新的对象,改变list本身

>>>a = []
>>>a.append(2)
>>>a
[2]
>>>a.append([3,4])
>>>a
[2,[3,4]]

3. L.extend(iterable)

extend可以在list的末尾追加另一个序列中的多个值

>>>a = [1,2,3]
>>>b = [4,5,6]
>>>a.extend(b)
>>>a
[1,2,3,4,5,6]
>>> member = [1,2,3]
>>> member.extend((4,5,6))
>>> member
[1, 2, 3, 4, 5, 6]    #可以将tuple里面的元素追加到list

与连接不同的是:extend方法改变了被扩展的list

>>>a = [1,2,3]
>>>b = [4,5,6]
>>>a + b
[1,2,3,4,5,6]
>>>a
[1,2,3]

4. L.insert(index, object)

insert用于将对象插入index之后,改变list本身

>>>numbers = [1,2,3,4,5]
>>>numbers.insert(0,'start')
>>>numbers
['start',1,2,3,4,5]
>>>len(numbers)
6
#当index>len(list)时,自动插入到最后
>>>a = [1, 2, 3]
>>>a.insert(4,4)
>>>a
[1, 2, 3, 4]
>>>a.insert(-5, -1)
>>>a
[-1, 1, 2, 3, 4]

5. L.pop()

pop用于移除list中的一个元素(默认最后一个),并且返回该元素
可指定位置,不存在的位置会报错

>>>a = [1,2,3]
>>>a.pop()
3
>>>a
[1,2]
>>>a.pop(0)
a
>>>a
[2]
>>>a.pop(100)
IndexError: pop index out of range

6. L.remove()

remove用于移除list中某个值的第一个匹配项
没有返回错误

>>>a = ['we','you','me','to','you']
>>>a.remove('me')
>>>a
['we', 'you', 'to', 'you']
>>>a.remove('you')
>>>a
['we','to','you']
>>>a.remove(3)
ValueError: list.remove(x): x not in list

7. L.count(value)

count用来统计某个元素在list中出现的次数
不存在则返回0

>>>a = [1,2,3,4,5,1]
>>>a.count(1)
2
>>>a.count(2)
1
>>>a.count(0)
0

8. L.index(value, [start, [stop]])

index返回某个值在list中第一次出现的索引

>>>a = [1,2,3,1,2,3]
>>>a.index(2)
1
#当这个值不在list中的时候,会触发一个异常
>>>a.index(0)
ValueError: 0 is not in list

9. L.reverse()

reverse将list中的元素反向存放,改变list本身

>>>a = [1,2,3]
>>>a.reverse()
>>>a
[3,2,1]

10. L.sort()

sort将list进行排序,改变list本身

>>>a = [3,-1,4,-3,7,2,0]
>>>a.sort()
>>>a
[-3, -1, 0, 2, 3, 4, 7]

11.L.clear()

清空

>>>a = [1, 2, 3, 4]
>>>a.clear()
>>>a
[]

相关文章

  • list mothod总结(python3.5)

    1. 创建一个空的list 2. L.append(object) append用于在list末尾追加一个新的对象...

  • 安装selenium

    mac os 10.11.5mac自带python2.7,自己下载了python3.5,pip list查看系统中...

  • json相关

    总结:[]-->List-->List{}-->LinkedHashMap-->_Interna...

  • List 总结

    List概述: 1、List :一个元素有序、可重复的集合。 集合中每个元素都有其对应的顺序索引。List 集合允...

  • List总结

    所有的实现,凡是按element不通过index查找,都需要遍历所有元素比较,性能不好。 没有按element排序...

  • List总结

    ArrayList 简介 ArrayList底层为Object数组, 当不提供参数实, 默认使用空数组, 由于ad...

  • java开发工程常用的数据结构(集合)

    List Map Set list,set,map对比 总结: Vector和ArrayList vector是线...

  • 如何彻底卸载Ubuntu16.04自带的Python3.5

    1.卸载python3.5 2.卸载python3.5以及它的依赖包 3.清除python3.5 要想清除pyth...

  • ptython FutureWarning

    /root/anaconda3/envs/python3.5/lib/python3.5/site-package...

  • 收藏贴

    Android样式开发之layer-list、level-list总结AndroidStudio中使用PlantU...

网友评论

      本文标题:list mothod总结(python3.5)

      本文链接:https://www.haomeiwen.com/subject/uzudhttx.html