<返回更多

python数据类型-列表(list)

2022-03-21    HyRer
加入收藏

Python/ target=_blank class=infotextkey>Python中列表是最常用的数据类型之一,由多个元素组成的集合,每个元素都有一个位置或者叫索引,索引的值从0开始,往后顺序递推,最大值为列表长度-1

例如

aa = [1, 2, 3, 4, 5]

print(aa[0])  # 1
print(aa[1])  # 2
print(aa[2])  # 3

常用方法

索引、切片

#  索引 切片
l = ['a', 'b', 'c', 'd', 'e', 'f']
# 优先掌握部分
print(l[1:5])
print(l[1:5:2])
print(l[2:5])
print(l[-1])
"""
['b', 'c', 'd', 'e']
['b', 'd']
['c', 'd', 'e']
f
"""
# 了解
print(l[-1:-4])
print(l[-4:-1])
print(l[-4:])
l = ['a', 'b', 'c', 'd', 'e', 'f']
print(l[-2:])
"""
[]
['c', 'd', 'e']
['c', 'd', 'e', 'f']
['e', 'f']
"""

追加元素Append()

hobbies = ['play', 'eat', 'sleep', 'study']
hobbies.append('girls')
print(hobbies) # ['play', 'eat', 'sleep', 'study', 'girls']

删除元素

pop()

如果pop()里面没加参数 则默认删除最后一个元素

hobbies = ['play', 'eat', 'sleep', 'study', 'run', "girl"]
x = hobbies.pop(1)  # 不是单纯的删除,是删除并且把删除的元素返回,我们可以用一个变量名去接收该返回值
print(x)
print(hobbies)

x = hobbies.pop(0)
print(x)

x = hobbies.pop(0)
print(x)

x = hobbies.pop()
print(x)
"""
eat
['play', 'sleep', 'study', 'run', 'girl']
play
sleep
girl
"""

del

del和pop() 不一样, 他没有返回值,只是单纯的将参数里面索引对应的元素从列表里面删除

这种删除方式不光在列表中有用,在后面的元组和字典里也是有用的

hobbies = ['play', 'eat', 'sleep', 'study']
del hobbies[1]  # 单纯的删除
print(hobbies)  # ['play', 'sleep', 'study']

remove()

remove()参数是具体的元素值,不是索引,也没有返回值

hobbies = ['play', 'eat', 'sleep', 'study']
hobbies.remove('eat')  # 单纯的删除,并且是指定元素去删除
print(hobbies)  # ['play', 'sleep', 'study']

用append()和pop()队列和堆栈

队列:是一种数据结构,其特点是先进先出,就和排队一样,排在最前面的人优先买到东西

堆栈: 是一种数据结构,其特点是后进先出,就和往桶里面放东西一样,最后放进去的,往往是最先拿出来

# 队列:先进先出
queue_l = []
# 入队
queue_l.append('first')
queue_l.append('second')
queue_l.append('third')
print(queue_l)  # ['first', 'second', 'third']
# 出队
print(queue_l.pop(0))  # first
print(queue_l.pop(0))  # second
print(queue_l.pop(0))  # third
# 堆栈:先进后出,后进先出
l = []
# #入栈
l.append('first')
l.append('second')
l.append('third')
# #出栈
print(l)  # ['first', 'second', 'third']
print(l.pop())  # third
print(l.pop())  # second
print(l.pop())  # first

计算列表的长度

hobbies=['play','eat','sleep','study']
print(len(hobbies))  # 4

列表是否包含某个元素

对于字符串来说也是可以的

#     包含in
hobbies = ['play', 'eat', 'sleep', 'study']
print('sleep' in hobbies)  # True

msg = 'hello world sz'
print('sz' in msg)  # True

插入元素insert()

hobbies = ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
hobbies.insert(1, 'walk')
print(hobbies)
hobbies.insert(1, ['walk1', 'walk2', 'walk3'])
print(hobbies)
"""
['play', 'walk', 'eat', 'sleep', 'study', 'eat', 'eat']
['play', ['walk1', 'walk2', 'walk3'], 'walk', 'eat', 'sleep', 'study', 'eat', 'eat']
"""

统计个数count()

hobbies = ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
print(hobbies.count('eat'))  # 3

列表合并extend()

list1.extend(list2) 将list2中的元素从list1的末尾添加上list1中

hobbies = ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
hobbies.extend(['walk1', 'walk2', 'walk3'])
print(hobbies)  
# ['play', 'eat', 'sleep', 'study', 'eat', 'eat', 'walk1', 'walk2', 'walk3']

获取某个元素的索引index()

如果列表中有当前元素,则返回当前元素第一次出现的索引
如果列表中没有当前元素,没有就报错
hobbies = ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
print(hobbies.index('eat'))  # 1
print(hobbies.index('girl'))  # 报错

清空元素clear()

hobbies = ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
hobbies.clear()
print(hobbies)  # []

列表拷贝copy()

列表拷贝属于浅拷贝, 修改列表里面的元素会相互影响,切记,这里不展开说,后面会详细说

hobbies = ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
l = hobbies.copy()  # ['play', 'eat', 'sleep', 'study', 'eat', 'eat']
print(l)


列表反转reverse()

l = [1, 2, 3, 4, 5]
l.reverse()
print(l)  # [5, 4, 3, 2, 1]

列表排序sort()

l = [100, 9, -2, 11, 32]
l.sort()
print(l)  # [-2, 9, 11, 32, 100]

l = [1, 4, 5, 2, 6]
l.sort(reverse=True)
print(l)  # [6, 5, 4, 2, 1]
声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>