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 ...
随机推荐
- 图解JAVA容器核心类库
JAVA容器详解 类继承结构图 HashMap 1. 对象的HashCode是用来在散列存储结构中确定对象的存储地址的. 2. 如果两个对象的HashCode相同,即在数组中的地址相同.而数组的元 ...
- 06.深入学习redis replication的完整流程和原理
一.replication的完整流程 slave配置master ip和port # slaveof <masterip> <masterport> slaveof 127.0 ...
- 1.OpenGL mac开发环境搭建记录
1.安装GLEW 和GLFW,转摘至:https://www.cnblogs.com/pretty-guy/p/11357793.html 2.开始测试,整个工程报错,关键信息如下: code sig ...
- webapi上传图片的两种方式
/// <summary> /// App上传图片 /// </summary> /// <returns>返回上传图片的 ...
- C003:计算球体体积 自行输入球体半径
程序: #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { float sphereRadius; do{ pri ...
- weekly-contest-205
weekly-contest-205 1 / 5507. 替换所有的问号 class Solution { public String modifyString(String s) { StringB ...
- 网易云uwp
起因 昨天晚上折腾Ubuntu 莫名其妙任务栏的网易云音乐图标消失了,今早才发现原来是更新了. but,这个更新真的是让人一言难尽 upw更新一下直接变成了桌面版? 折腾 重新装回老版uwp 网易云U ...
- Linux:安装php
一.环境准备(lamp环境:linux+apache+php+mysql) 1.安装php之前,首先要检查一下相关lib库 rpm -qa zlib libxml libjpeg freetype ...
- 本周 GitHub 速览:自动化当道,破密、爬虫各凭本事
作者:HelloGitHub-小鱼干 摘要:安全门外汉,如何在不知道密钥或密码的情况下,破解哈希得到原文,Ciphey 会告诉你当中的密码.说到 auto 智能爬虫会基于上一次的爬虫经历进一步学习以获 ...
- 最优得分 score
Solution: 一道很典型的dp题目, 对于 20% 的数据(Bi=0),直接使用01背包即可. 对于 100% 的数据,我们需要进行分析,当我们对 …a,b… 和 …b,a… (…表示的是相同的 ...