Python列表,元组

作者: 大白python学习分享 | 来源:发表于2018-06-18 18:24 被阅读0次

1.列表

1.len/index/count/列表[索引]取值

image.png

len(list)取列表的长度
list.index("字符串")取下标/索引
list.count("字符串")查看字符串在列表中出现了几次
list[索引]通过索引取值

2.sort/sort(reverse=True)/reverse.()

image.png

list.sort() 排序
list.sort(reverse=True) 降序
list.reverse() 倒序

3.del/remove/pop

In [12]: list
Out[12]: ['1', '2', '3', '4', '5', '6', '7', '8', '9']

In [13]: del list[4]

In [14]: list
Out[14]: ['1', '2', '3', '4', '6', '7', '8', '9']


In [28]: list
Out[28]: ['1', '2', '3', '4', '6', '7', '8', '9']

In [29]: list.remove("2")

In [30]: list
Out[30]: ['1', '3', '4', '6', '7', '8', '9']


In [31]: list
Out[31]: ['1', '3', '4', '6', '7', '8', '9']

In [32]: list.pop()
Out[32]: '9'

In [33]: list
Out[33]: ['1', '3', '4', '6', '7', '8']


In [34]: list.pop(2)
Out[34]: '4'

In [35]: list
Out[35]: ['1', '3', '6', '7', '8']

del list[索引] 删除指定索引数据
list.remove(数据/值) 删除指定数据
list.pop() 默认删除末尾数据
list.pop(索引) 删除指定索引数据

3.insert/append/extend

In [36]: list
Out[36]: ['1', '3', '6', '7', '8']

In [37]: list.insert(2,"as")

In [38]: list
Out[38]: ['1', '3', 'as', '6', '7', '8']

In [39]: list.append('wo ai ni xiao zhu')

In [40]: list
Out[40]: ['1', '3', 'as', '6', '7', '8', 'wo ai ni xiao zhu']

In [41]: list1=['q','w','e']

In [42]: list.extend(list1)

In [43]: list
Out[43]: ['1', '3', 'as', '6', '7', '8', 'wo ai ni xiao zhu', 'q', 'w', 'e']

list.insert(索引,数据) 在指定索引处插入数据
list.append(数据) 在列表末尾插入数据
list.extend(列表) 把列表1里面的内容追加到列表中

2.元组

创建空元组是
tuple=()
注意:是小括号
元组是不可以更改的,里面的内容不可以修改

In [45]: tuple=('as','asd','qw','12')

In [46]: type(tuple)
Out[46]: tuple

In [47]: len(tuple)
Out[47]: 4

In [48]: tuple.count("as")
Out[48]: 1

In [49]: tuple[1]
Out[49]: 'asd'

In [50]: tuple.index('qw')
Out[50]: 2

len(元组) 取元组的长度
tuple.count(数据) 数据在元组中出现的次数
tuple[索引] 通过索引取数据
tuple.index(数据) 通过数据取索引

相关文章

  • python学习——元组

    Python —— 元组 元组与列表极为相似,列表以【】表示,元组以()表示。 列表可以修改其中的元素,元组不可修...

  • python 基础 - 元组

    Python 元组 Python 的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号...

  • 元祖

    Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。...

  • Python基础之元组、字典,集合详解

    之前总结了Python列表,这篇总结Python的元组,字典和集合。 一 元组 tuple Python 的元组与...

  • Python TUPLE - 打包,解包,比较,切片,删除,键

    什么是Python的中的元组? 元组就像一系列不可变Python对象的列表。列表和元组之间的区别在于列表在方括号中...

  • 第3章:内建数据结构、函数及文件

    python的常用数据结构:元组、列表、字典和集合 元组(tuple):固定长度、不可变的python序列 列表:...

  • Lesson 016 —— python 元组

    Lesson 016 —— python 元组 Python 的元组与列表类似,不同之处在于元组的元素不能修改。 ...

  • 14.元组

    Python的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。python中不允许...

  • Day-07 preview

    元组 tuple[tʌpl] Python 的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表...

  • Python基础_05:元组(2019-1-14)

    元组 python中元组和列表类似不同之处在于元组中的元素不可修改元组使用(),列表使用[]return a,b,...

网友评论

    本文标题:Python列表,元组

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