python队列操作

news/2024/9/18 20:42:17 标签: 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)
[]


http://www.niftyadmin.cn/n/5664492.html

相关文章

力扣150题——位运算

位运算概述 位运算(Bitwise Operation)是计算机底层操作中的一种,用来直接对整数的二进制位进行操作。位运算通常速度很快,且消耗的内存较少,在处理一些特定问题(如加密算法、图像处理、低级硬件编程等&…

Threejs之看房案例(下)

本文目录 前言最终效果1、点精灵1.1 添加点精灵1.2 点精灵效果2、添加事件2.1 鼠标移动事件2.1.1 效果2.2 鼠标点击事件2.2.1 效果2.3 切换互通3. 完整代码前言 在Threejs之看房案例(上)这篇博客中我们已经完成了大厅的3d观看效果,但是我们会发现如果想去其他房间观看,没有…

好用的超声波清洗机有哪些?精选四大爆款品牌汇总

随着时代的发展及生活水平的提升,珠宝饰品、眼镜等个人物品日益普及至千家万户。然而,这些贵重小物在日常存放中难免会积累微尘与隐形细菌,无形中可能对我们的健康产生潜在影响。鉴于细菌的微小难察,超声波清洗机应运而生&#xf…

进程监控与管理详解

一、进程的定义: 进程process是正在运行的程序,包括: 分配的内存地址空间 安全属性、包括所有权和特权 一个或多个线程 进程状态 进程的环境包括: 本地和全局变量 当前调度上下文…

【每日一题】LeetCode 815.公交路线(广度优先搜索、数组、哈希表)

【每日一题】LeetCode 815.公交路线(广度优先搜索、数组、哈希表) 题目描述 给定一个表示公交线路的数组 routes,其中每个 routes[i] 表示第 i 辆公交车的循环行驶路线。现在从 source 车站出发,要前往 target 车站,…

Linux | 进程间通信:管道、消息队列、共享内存与信号量

文章目录 《深入理解进程间通信:管道、消息队列、共享内存与信号量》一、进程间通信介绍(一)进程间通信目的(二)进程间通信发展(三)进程间通信分类 二、管道(一)什么是管…

C++:字符串string转成整型int

一、atoi atoi 是 C 标准库中的一个函数,全称是 ASCII to Integer,用于将字符串转换为整数。 函数定义 int atoi(const char *str);参数:str 是一个指向以 \0 结尾的字符串的指针。返回值:返回字符串转换后的整数。如果字符串中…

Flutter Android Package调用python

操作步骤 一、创建一个Flutter Package 使用以下指令创建一个Flutter Package flutter create --templateplugin --platformsandroid,ios -a java flutter_package_python 二、修改android/build.gradle文件 在buildscript——>dependencies中添加以下内容 //导入Chaqu…