list在python中是什么意思-Python教程

资源魔 39 0

序列是Python中最根本的数据构造。序列中的每一个元素都调配一个数字 - 它的地位,或索引,第一个索引是0,第二个索引是1,依此类推。

Python有6个序列的内置类型,但最多见的是列表以及元组。

序列均可以进行的操作包罗索引,切片,加,乘,反省成员。

别的,Python曾经内置确定序列的长度和确定最年夜以及最小的元素的办法。

列表是最罕用的Python数据类型,它能够作为一个方括号内的逗号分隔值呈现。

列表的数据项没有需求具备相反的类型

创立一个列表,只需把逗号分隔的没有同的数据项应用方括号括起来便可。以下所示:

list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]

与字符串的索引同样,列表索引从0开端。列表能够进行截取、组合等。

拜访列表中的值:

应用下标索引来拜访列表中的值,一样你也能够应用方括号的方式截取字符,以下所示:

#!/usr/bin/python

list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7 ]

print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

以上实例输入后果:

list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]

更新列表

你能够对列表的数据项进行修正或更新,你也能够应用append()办法来增加列表项,以下所示:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

list = []          ## 空列表
list.append('Google')   ## 应用 append() 增加元素
list.append('Runoob')
print list

以上实例输入后果:

['Google', 'Runoob']

删除了列表元素

能够应用 del 语句来删除了列表的元素,以下实例:

#!/usr/bin/python
list1 = ['physics', 'chemistry', 1997, 2000]
print list1
del list1[2]
print "After deleting value at index 2 : "
print list1

以上实例输入后果:

['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]

更多Python相干技巧文章,请拜访Python教程栏目进行学习!

以上就是list正在python中是甚么意义的具体内容,更多请存眷资源魔其它相干文章!

标签: python教程 python编程 python使用问题 list在python中是什么

抱歉,评论功能暂时关闭!