python基础之列表操作

Posted 水木,年華

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础之列表操作相关的知识,希望对你有一定的参考价值。

为什么需要列表

a=10 #变量存储的是一个对象的引用
lst=['hello','world',98]
print(id(lst))
print(type(lst))
print(lst)
#输出结果
2652627143168
<class 'list'>
['hello', 'world', 98]

列表的创建

'''创建列表的第一种方式,使用[]'''
lst=['hello','world',98]

'''创建列表的第二种方式,使用内函数list()'''
lst2=list(['hello','world',98])

列表的特点

列表的查询操作

●获取列表中指定元素的索引

lst=['hello','world',98,'hello']
print(lst.index('hello'))      # 如果列表中有相同元素只返回列表中相同元素的第一个元素索引
#print(lst.index('python'))    # ValueError: 'python' is not in list
#print(lst.index('hello',1,3)) # ValueError: 'hello' is not in list
print(lst.index('hello',1,4))  #3

●获取列表中的单个元素

lst=['hello','hello','98','hello','world',234]
#获取索引为2的元素
print(lst[2])        #98
#获取索引为-3的元素
print(lst[-3])       #hello
#获取索引为10的元素
print(lst[10])       #IndexError: list index out of range

●获取列表的多个元素

lst=[10,20,30,40,50,60,70,80]
print('原列表',id(lst))
lst2=lst[1:6:1]
print(lst2)
print('切的片段:',id(lst2))
print(lst[1:6:2])
#输出结果
原列表 2648804755968
[20, 30, 40, 50, 60]
切的片段: 2648804753920
[20, 40, 60]

print('------------------step步长为负数的情况------------------------')
print('原列表:',lst)
print(lst[::-1])
#start=7,stop 省略 step=-1
print(lst[7::-1])
#start=6,stop=0,step=-2
print(lst[6:0:-2])
#输出结果
原列表: [10, 20, 30, 40, 50, 60, 70, 80]
[80, 70, 60, 50, 40, 30, 20, 10]
[80, 70, 60, 50, 40, 30, 20, 10]
[70, 50, 30]

●判断指定元素在列表中是否存在

lst=[10,20,'python','hello']
print(10 in lst)
print(100 in lst)
print(10 not in lst)
print(100 not in lst)
for item in lst:
    print(item)
#输出结果
True
False
False
True
10
20
python
hello

●列表元素的增加操作

#向列表的末尾添加一个元素
lst=[10,20,30]
print('添加元素之前',lst,id(lst))
lst.append(100)
print('添加元素之后',lst,id(lst))
lst2=['hello','world']
lst.append(lst2) #将lst2作为一个元素添加到列表的末尾
print(lst)
#输出结果
添加元素之前 [10, 20, 30] 2111305550336
添加元素之后 [10, 20, 30, 100] 2111305550336
[10, 20, 30, 100, ['hello', 'world']]
#######################################################################################
#lst.append(lst2) #将lst2作为一个元素添加到列表的末尾
lst.extend(lst2) #向列表的末尾一次性添加多个元素
print(lst)
[10, 20, 30, 100, 'hello', 'world']
########################################################################################
#在任意位置上添加一个元素
lst.insert(1,90)
print(lst)
########################################################################################
#在任意的位置上添加N多个元素(切片)
print('切片之前',lst)
lst3=['a','b','c']
lst[1:]=lst3
print('切片之后',lst)
切片之前 [10, 90, 20, 30, 100, 'hello', 'world']
切片之后 [10, 'a', 'b', 'c']

●列表元素的删除

lst=[10,20,30,40,50,60,30]
lst.remove(30)  #从列表中移除第一个元素,如果有重复元素只移除第一个元素
print(lst)
lst.remove(100) #ValueError: list.remove(x): x not in list
[10, 20, 40, 50, 60, 30]
lst=[10,20,30,40]
#pop()根据索引移除元素
print('移除之前列表为',lst)
lst.pop(1)
print(lst)
#lst.pop(5) #IndexError: pop index out of range
lst.pop() #如果不指定参数(索引),将删除列表中的最后一个元素
print(lst)

以上是关于python基础之列表操作的主要内容,如果未能解决你的问题,请参考以下文章

python基础之列表操作

python基础之序列类型的方法——列表&amp;amp;元组

python基础之 列表元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码

Python 6-1.内置数据结构之list(基础篇)

Python基础之元组tuple(带了枷锁的列表)

Python基础之列表