Python-列表 元组-list tuple
列表 list
[vale,...] 可加入任意类型数据,并可嵌套,不定长
student = ["beimenchuixue", "maYun", "maHuiTeng", "naiChaDong"]
product = ["blog", "taoBao", "weiChat", "JD"]
score = ["beimenchuixue", [90, 80, 66, 77, 99]]
列表访问
1. 指定插入 .insert
2. 末尾插入 .append
3. 知值删 .remove
4. 知索引删,并返回值 .pop del
5. 清空列表 .clear
6. 反转列表 .reverse
7. 排序列表 .sort
8. 扩展列表 .extend
9. 拷贝列表 .copy
10. 知值查索引 index
11. 定义空列表 [] list()
product = ["boKeYuan", "taoBao", "weiChat", "jd", "baiDuYun", "aliYun"]
product.insert(1, "jinRiTouTiao")
print(product) product.append("douYin")
print(product) product.remove("jd")
print(product) del_product = product.pop(0)
print(del_product)
print(product) product.reverse()
print(product) # product.sort(lambda x: x[0], )
# print(product) two_product = ["weiRuan", "Centos", "Nginx"]
product.extend(two_product)
print(product) copy_product = product.copy()
print(copy_product) print(copy_product.index("aliYun")) copy_product.clear()
print(copy_product) empty_list = []
empty_list_two = list()
print(empty_list, empty_list_two) print(product.count("aliYun")) tuple_product = ("boKeYuan", "taoBao", "weiChat", "jd", "baiDuYun", "aliYun")
print(tuple_product.index("weiChat"))
print(tuple_product.count("weiChat"))
元组 tuple
(value, ...)
1. 声明数据不可变,避免隐藏的错误,固定不变的数据推荐使用元组
2. 可加入任意类型数据,并可嵌套,单个值需要加上 ,
3. 定义空元组 () tuple()
tuple_product = ("boKeYuan", "taoBao", "weiChat", "jd", "baiDuYun", "aliYun")
print(tuple_product.index("weiChat"))
print(tuple_product.count("weiChat"))
4. 不可变性是相对的
names = ("BeiMenChuiXue", [18, 175])
names[1][1] = 180
print(names)
5. 拆包,隐含位置信息
name, age, height = ("BeiMenChuiXue", 18, 175)
print(name, age, height)
name, *other = ("BeiMenChuiXue", 18, 175)
print(name, other)
tuple 比 list好的地方
1.不可变对象
a. 性能优化(作为常量在编译时确认)
b. 线程安全
c. 可以作为dict的key, 可hash对象才可以做字典key
d. 拆包特性
2. 用C语言做类别,Tuple对应的struct,而list对应则是array
() 的意思
1. 强制或强调优先级顺序
2. 数学运算 type((5)) 结果为 int
3. 空元组 () 和元组 (value, ...)
4. 生成器 () + for
5. 正则表达式当作一个字符,并取其匹配的字符 (re)
result = not True or True result_two = not (True or True)
print(result, result_two) print(type((6 + 7))) print(type(())) generator = (i for i in range(10))
print(generator.__next__()) one_str = "Simple is better than complex."
regular = r"\w+"
import re
find_list = re.findall(regular, one_str)
print(find_list)
Python-列表 元组-list tuple的更多相关文章
- python列表元组
python列表元组 索引 切片 追加 删除 长度 循环 包含 定义一个列表 my_list = [] my_list = list() my_list = ['Michael', ' ...
- Python—列表元组和字典
Python-列表元组和字典 列表 元组 字典 列表: 列表是Python中的一种数据结构,他可以存储不同类型的数据.尽量存储同一种类型 列表索引是从0开始的,我们可以通过索引来访问列表的值. 列表的 ...
- python 列表 元组 字典 集合
列表 lst = [i for i in range(10)] 切片 # 把下标小于2的显示出来 print(lst[:2]) # 把10个数有大到小输出 print(lst[::-1]) # 把下标 ...
- Python 列表&元组&字典&集合
列表(list) 有序性,可存储任意类型的值 通过偏移存取,支持索引来读取元素,第一个索引为0 ,倒数第一个索引为-1 可变性 ,支持切片.合并.删除等操作 可通过索引来向指定位置插入元素 可通过po ...
- 【277】◀▶ Python 列表/元组/字典说明
目录: 前言 一.访问列表中的值 二.更新列表 三.删除列表元素 四.Python 列表脚本操作符 五.Python 列表函数 & 方法 参考:Python 列表(List)使用说明 列表截取 ...
- Python 列表/元组/字典总结
序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列 ...
- Python列表,元组,字典,字符串方法笔记
01. 列表 1.1 列表的定义 List(列表) 是 Python 中使用 最频繁 的数据类型,在其他语言中通常叫做 数组 专门用于存储 一串 信息 列表用 [] 定义,数据 之间使用 , 分隔 列 ...
- python—列表,元组,字典
——列表:(中括号括起来:逗号分隔每个元素:列表中的元素可以是数字,字符串,列表,布尔值等等) (列表元素可以被修改) list(类) (有序的) [1]索引取值:切片取值:for循环:whi ...
- Python数据类型--元组(tuple)
元组与列表非常相似,最大区别在于: (1)元组是不可修改的,定义之后就"固定"了. (2)元组在形式上是用()这样的圆括号括起来 (3)元组不能插入或删除元素 注:元素可修改与不可 ...
- 零基础学Python:元组(Tuple)详细教程
Python的元组与列表类似,不同之处在于元组的元素不能修改,元组使用小括号,列表使用方括号,元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可https://jq.qq.com/?_wv=1 ...
随机推荐
- SpringMVC中前端Form表单提交后跳转不过去的问题
今天晚上打算谢谢Spring整合这个内容,写的差不多之后运行 出现了各种问题 逐一排查 首先有一个(MyEclipse10) 一: class path resource [spring-mvc.xm ...
- VUE+ElementUI创建项目
1.官网下载node,安装node.js环境 安装完成后进入cmd,输入node -v和npm -v查看node和npm是否安装成功及对应的版本 2.全局安装vue-cli:cnpm install ...
- vue组件获取和vue-cli的基本了解
Vue获取组件的一些方法 this.$refs.xxx 给标签绑定ref属性,获取的是当前DOM对象 给组件绑定ref属性,获取的是组件实例对象 this.$parent 获取当前组件的父组件,为一个 ...
- Python协程之asyncio
asyncio 是 Python 中的异步IO库,用来编写并发协程,适用于IO阻塞且需要大量并发的场景,例如爬虫.文件读写. asyncio 在 Python3.4 被引入,经过几个版本的迭代,特性. ...
- tomcat服务器java.lang.OutOfMemoryError: PermGen space
一挂就报内存溢出 下面是TOMCAT日志 JAVA程序是没有报错, Nov 24, 2009 4:07:02 PM org.apache.catalina.core.ApplicationDispat ...
- SDWebImage 清除磁盘缓存机制 iOS
分析的版本 pod 'SDWebImage', '~> 5.0.6' SDWebImage默认清除磁盘缓存的时长是7天. /** * The maximum length of time to ...
- 从String中移除空白字符的多种方式!?
字符串,是Java中最常用的一个数据类型了.我们在日常开发时候会经常使用字符串做很多的操作.比如字符串的拼接.截断.替换等. 这一篇文章,我们介绍一个比较常见又容易被忽略的一个操作,那就是移除字符串中 ...
- FastDFS+nginx整合模块安装
FastDFS安装 安装gcc编译器 yum -y install gcc automake autoconf libtool make 上传解压两个文件包 libfastcommon是从FastDF ...
- JVM学习第二天(垃圾回收器和内存分配策略)大章
说道垃圾回收器大家应该都会有所了解,GC白,当然说道具体的可能就不是很清楚了,今天我们就来玩一玩; GC要做的事情: 第一步:确定堆中需要回收的对象; 第二步:什么时候回收; 第三步:怎样回收 为什么 ...
- 查看CentOs6.5/7的系统版本号
在centos6.5上用 [root@msg45 ~]# lsb_release -aLSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0- ...