python队列操作

1. 队列初始化

创建空的队列:

python 复制代码
test_list = []

使用初始值:

python 复制代码
test_list = [1, 2, 3, 4, 5]
test_list3 = ["a", "b", "c", "d"]

使用列表生成式创建一个带有初始元素的列表:

python 复制代码
>>> test_list = [x for x in range(3, 15, 2)]
>>> print(test_list)
[3, 5, 7, 9, 11, 13]

将字典的key转换为队列:

python 复制代码
>>> test_dict = {"one":1,"two":2,"three":3}
>>> print(list(test_dict.keys()))
['one', 'two', 'three']

将字典的value都转换为队列:

python 复制代码
>>> test_dict = {"one":1,"two":2,"three":3}
>>> print(list(test_dict.values()))
[1, 2, 3]

将字典的key和value都转换为队列:

python 复制代码
>>> test_dict = {"one":1,"two":2,"three":3}
>>> print(list(test_dict.items()))
[('one', 1), ('two', 2), ('three', 3)]

2. 判断一个元素是否在队列中:

index函数,返回元素在队列中的位置:

python 复制代码
test_list = [1, 2, 3, 4, 5]
try:
	print(test_list.index(3))
except Exception as e:
	print('队列中不存在!')

count函数,统计元素在队列中的次数:

python 复制代码
test_list = [1, 2, 3, 4, 5]
print(test_list.count(3))

通过in来判断:

python 复制代码
test_list = [1, 2, 3, 4, 5]
a = 3
if a in test_list :
    print("a在队列中")
else:
    print("a不在队列中")

3. 向队列中加入新元素:

append函数,在队列尾部追加:

python 复制代码
>>> test_list = [1,2,3,4]
>>> test_list.append(5)
>>> test_list.append(6)
>>> test_list.append(7)
>>> print(test_list)
[1, 2, 3, 4, 5, 6, 7]

insert函数,在队列特定位置插入:

python 复制代码
>>> test_list = [1,2,3,4]
>>> test_list.insert(2,9)
>>> print(test_list)
[1, 2, 9, 3, 4]

更改队列特定位置的值:

python 复制代码
>>> test_list = [1, 2, 3, 4, 5]
>>> test_list[3]=7
>>> print(test_list)
[1, 2, 3, 7, 5]

4. 向队列中追加另外一个队列:

extend函数,在队列的末尾追加新队列:

python 复制代码
>>> test_list = [1, 2, 3, 4, 5]
>>> test_list2 = [6, 7]
>>> test_list.extend(test_list2)
>>> print(test_list)
[1, 2, 3, 4, 5, 6, 7]

将两个队列合并:

python 复制代码
>>> test_list = [1, 2, 3, 4, 5]
>>> test_list2 = [6, 7]
>>> test_list3 = test_list + test_list2
>>> print(test_list3)
[1, 2, 3, 4, 5, 6, 7]

5. 遍历队列:

python 复制代码
>>> test_list = [1, 2, 3, 4, 5]
>>> for a in test_list:
...     print(a)
...
1
2
3
4
5

反向遍历队列:

python 复制代码
>>> test_list = [1, 2, 3, 4, 5]
>>> for a in reversed(test_list):
...     print(a)
...
5
4
3
2
1

6. 队列排序:

sort(cmp=None, key=None, reverse=False)函数,升序排列:

python 复制代码
>>> test_list = [2, 1, 3, 5, 6, 8, 9, 7, 0]
>>> test_list.sort()
>>> print(test_list)
[0, 1, 2, 3, 5, 6, 7, 8, 9]

降序排列,使用sort(reverse=True):

python 复制代码
>>> test_list = [2, 1, 3, 5, 6, 8, 9, 7, 0]
>>> test_list.sort(reverse=True)
>>> print(test_list)
[9, 8, 7, 6, 5, 3, 2, 1, 0]

7. 获取队列长度:

python 复制代码
>>> test_list = [2, 1, 3, 5, 6, 8, 9, 7, 0]
>>> len(test_list)
9

8. 获取队列特定位置元素的值:

python 复制代码
>>> test_list = [1, 2, 3, 4, 5]
>>> print(test_list[3])
4

9. 删除队列中的元素:

remove函数,删除队列中匹配到某个值的第一个元素

python 复制代码
>>> test_list = [2, 1, 3, 5, 6, 8, 9, 7, 0]
>>> test_list.remove(2)
>>> print(test_list)
[1, 3, 5, 6, 8, 9, 7, 0]

pop函数,删除队列中指定位置的元素,并返回该元素

python 复制代码
>>> test_list = [2, 1, 3, 5, 6, 8, 9, 7, 0]
>>> test_list.pop(2)
3
>>> print(test_list)
[2, 1, 5, 6, 8, 9, 7, 0]

pop参数为空时,删除队列中最后一个元素,并返回该元素

python 复制代码
>>> test_list = [2, 1, 3, 5, 6, 8, 9, 7, 0]
>>> test_list.pop()
0
>>> print(test_list)
[2, 1, 3, 5, 6, 8, 9, 7]

10. 清除队列中的元素:

python 复制代码
>>> test_list = [2, 1, 3, 5, 6, 8, 9, 7, 0]
>>> test_list.clear()
>>> print(test_list)
[]
相关推荐
pan_junbiao6 分钟前
Vue使用axios二次封装、解决跨域问题
前端·javascript·vue.js
chusheng184014 分钟前
如何使用 Python 的 sqlite3 模块操作 SQLite 数据库?
数据库·python·sqlite
秋沐17 分钟前
vue3中使用el-tree的setCheckedKeys方法勾选失效回显问题
前端·javascript·vue.js
计算机编程-吉哥18 分钟前
计算机毕业设计 基于Python的美术馆预约系统的设计与实现 Python+Django+Vue 前后端分离 附源码 讲解 文档
python·django·毕业设计·毕业论文·计算机毕业设计·计算机毕业设计选题·美术馆预约系统
浮华似水34 分钟前
Yargs里的Levenshtein距离算法
前端
阿华的代码王国35 分钟前
【JavaEE】——多线程(join阻塞,计算,引用,状态)
java·开发语言·数据结构·java-ee
追着梦的码怪1 小时前
简单水印通过python去除
python·opencv
边疆.1 小时前
数据结构:内部排序
c语言·开发语言·数据结构·算法·排序算法
William数据分析1 小时前
[Python数据可视化]Plotly Express: 地图数据可视化的魅力
python·信息可视化·plotly·数据分析
William数据分析1 小时前
[Python数据可视化]探讨数据可视化的实际应用:三个案例分析
python·信息可视化·数据分析·数据可视化