Python入门系列(四)别再傻傻分不清:列表、元组、字典、集合的区别
总结分析列表、元组、字典、集合的相同与区别之处,只有彻底分清之后,就会在应用的时候,得心应手。
四句话总结
- 列表是一个有序且可更改的集合,允许重复成员。
- 元组是一个有序且不可更改的集合,允许重复成员。
- 集合是一个无序、不可更改*且未索引的集合,没有重复成员。
- 字典是一个有序且可更改的集合,没有重复成员。
公有的部分
获取长度,使用len()
要确定列表中有多少项,请使用len()函数
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
要确定一个元组有多少项,请使用len()函数
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
要确定一个集合有多少项,请使用len()函数。
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
要确定字典有多少项,请使用len()函数
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(len(thisdict))
通过引用索引号来访问
列表项已编制索引,您可以通过引用索引号来访问它们
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
您可以通过引用方括号内的索引号来访问元组项
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
是否存在指定项,请使用in关键字
要确定列表中是否存在指定项,请使用in关键字
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
要确定元组中是否存在指定项,请使用in关键字
thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
print("Yes, 'apple' is in the fruits tuple")
检查集合中是否有“香蕉”
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)
要确定字典中是否存在指定的键,请使用in关键字
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
可以使用for循环遍历
可以使用for循环遍历列表项
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
可以使用for循环遍历元组项
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
在集合中循环,并打印值
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
循环字典
for x in thisdict:
print(thisdict[x])
还可以使用values()方法返回字典的值
for x in thisdict.values():
print(x)
可以使用keys()方法返回字典的键
for x in thisdict.keys():
print(x)
使用items()方法循环遍历键和值
for x, y in thisdict.items():
print(x, y)
clear()方法清空
clear()方法清空列表。
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
clear()方法清空集合
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
clear()方法清空字典
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)
del关键字
del关键字还会删除指定的索引
thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)
del关键字也可以完全删除列表。
thislist = ["apple", "banana", "cherry"]
del thislist
del关键字将完全删除集合
thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset)
del关键字删除字典具有指定键名的项
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
remove()方法
remove()方法删除指定的项。
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
要删除集合中的项,请使用remove()或discard()方法。
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
pop()方法
pop()方法删除列表指定的索引。
thislist = ["apple", "banana", "cherry"]
thislist.pop(1)
print(thislist)
您也可以使用pop()方法删除一个项目,但此方法将删除最后一个项目。请记住,集合是无序的,因此您将不知道删除了哪些项。
thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)
pop()方法移除字典具有指定键名的项
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
列表
insert()方法在指定的索引处插入项
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)
要将项目添加到列表的末尾,请使用append()方法
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
要将其他列表中的元素附加到当前列表,请使用extend()方法。
thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend(tropical)
print(thislist)
extend()方法不必附加列表,您可以添加任何可迭代对象(元组、集合、字典等)。
thislist = ["apple", "banana", "cherry"]
thistuple = ("kiwi", "orange")
thislist.extend(thistuple)
print(thislist)
如果不指定索引,则pop()方法将删除最后一项。
thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)
列表理解提供了循环列表的最短语法:newlist = [*expression* for *item* in *iterable* if *condition* == True]
thislist = ["apple", "banana", "cherry"]
[print(x) for x in thislist]
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)
newlist = [x.upper() for x in fruits]
列表对象有一个sort()方法,默认情况下,该方法将按字母数字升序对列表进行排序
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
在排序列表时,我们可以使用内置函数作为关键函数
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.sort(key = str.lower)
print(thislist)
reverse()方法反转元素的当前排序顺序。
thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.reverse()
print(thislist)
有多种方法可以复制,一种方法是使用内置的列表方法copy()。
您不能简单地通过键入list2=list1复制列表,因为:list2仅仅是对list1的引用,并且在list1中所做的更改也将自动在list2中进行。
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
制作副本的另一种方法是使用内置方法list()。
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
在Python中,有几种方法可以连接或串联两个或多个列表。最简单的方法之一是使用+运算符。
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
也可以使用extend()方法,其目的是将元素从一个列表添加到另一个列表
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
元组
要创建只有一个项的元组,必须在该项后添加逗号,否则Python将无法将其识别为元组。
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
您可以将元组转换为列表,更改列表,然后将列表转换回元组。
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
将元组添加到元组。您可以将元组添加到元组中,因此如果要添加一个(或多个)项,请使用该项创建一个新元组,并将其添加到现有元组中.
thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y
print(thistuple)
我们可以将值提取回变量中,这称为“拆包”
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
如果变量的数量小于值的数量,则可以在变量名中添加*号,这些值将作为列表分配给变量
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
(green, yellow, *red) = fruits
print(green)
print(yellow)
print(red)
要连接两个或多个元组,可以使用+运算符
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
如果要将元组的内容乘以给定的次数,可以使用*运算符
fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2
print(mytuple)
集合
创建集后,不能更改其项,但可以添加新项。
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
要将其他集合中的项添加到当前集合中,请使用update()方法。
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
可以使用union()方法返回包含两个集合中所有项的新集合,也可以使用update()方法将一个集合中的所有项插入另一个集合
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
intersection_update()方法将只保留两个集合中存在的项。
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y)
print(x)
intersection()方法将返回一个新的集合,该集合只包含两个集合中存在的项。
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z)
symmetric_difference_update()方法将只保留两个集合中不存在的元素。
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.symmetric_difference_update(y)
print(x)
symmetric_difference()方法将返回一个新的集合,该集合只包含两个集合中不存在的元素。
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z)
字典
您可以通过在方括号内引用字典的键名来访问字典的项
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
还有一个名为get()的方法,它将给出相同的结果
x = thisdict.get("model")
keys()方法将返回字典中所有键的列表。
x = thisdict.keys()
values()方法将返回字典中所有值的列表。
x = thisdict.values()
items()方法将返回字典中的每个项,作为列表中的元组。
x = thisdict.items()
返回的列表是字典项的视图,这意味着对字典所做的任何更改都将反映在项列表中。
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.items()
print(x) #before the change
car["year"] = 2020
print(x) #after the change
popitem()方法删除最后插入的项(在3.7之前的版本中,将删除随机项)
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict)
您不能简单地通过键入dict2=dict1来复制字典,因为:dict2将仅是对dict1的引用,在dict1中所做的更改也将自动在dict2中进行。
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)
字典可以包含字典,这称为嵌套字典。
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
child1 = {
"name" : "Emil",
"year" : 2004
}
child2 = {
"name" : "Tobias",
"year" : 2007
}
child3 = {
"name" : "Linus",
"year" : 2011
}
myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3
}
您的关注,是我的无限动力!
公众号 @生活处处有BUG
Python入门系列(四)别再傻傻分不清:列表、元组、字典、集合的区别的更多相关文章
- python中列表 元组 字典 集合的区别
列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计 ...
- python 中列表 元组 字典 集合的区别
先看图片解释 (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计过去一周我们买过的东西,把这些东西列出来,就是清单.由于我们买一种东西可能不止一次,所以清单中是允许有重复 ...
- Python第三天 序列 5种数据类型 数值 字符串 列表 元组 字典 各种数据类型的的xx重写xx表达式
Python第三天 序列 5种数据类型 数值 字符串 列表 元组 字典 各种数据类型的的xx重写xx表达式 目录 Pycharm使用技巧(转载) Python第一天 安装 shell ...
- **python中列表 元组 字典 集合
列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. 1.列表 列表是以方括号“[]”包围的数据集合,不同成员以“,”分隔. 列表的特 ...
- python的学习笔记01_4基础数据类型列表 元组 字典 集合 其他其他(for,enumerate,range)
列表 定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 特性: 1.可存放多个值 2.可修改指定索引位置对应的值,可变 3.按照从左到右的顺序定义列表元素,下标从0开始顺序访问 ...
- Python入门基础学习(列表/元组/字典/集合)
Python基础学习笔记(二) 列表list---[ ](打了激素的数组,可以放入混合类型) list1 = [1,2,'请多指教',0.5] 公共的功能: len(list1) #/获取元素 lis ...
- python 列表 元组 字典 集合
列表 lst = [i for i in range(10)] 切片 # 把下标小于2的显示出来 print(lst[:2]) # 把10个数有大到小输出 print(lst[::-1]) # 把下标 ...
- Python 列表&元组&字典&集合
列表(list) 有序性,可存储任意类型的值 通过偏移存取,支持索引来读取元素,第一个索引为0 ,倒数第一个索引为-1 可变性 ,支持切片.合并.删除等操作 可通过索引来向指定位置插入元素 可通过po ...
- Python列表,元组,字典,集合详细操作
菜鸟学Python第五天 数据类型常用操作及内置方法 列表(list) ======================================基本使用====================== ...
- Python数据类型-布尔/数字/字符串/列表/元组/字典/集合
代码 bol = True # 布尔 num = 100000000; # 数字 str = "fangbei"; # 字符串 str_cn = u"你好,方倍" ...
随机推荐
- Caller 服务调用 - Dapr
前言 上一篇我们讲了使用HttpClient的方式调用,那么如果我们现在需要更换为通过dapr实现服务调用,我们需要做哪些事情呢? Caller.Dapr 入门 如果我们的项目原本使用的是Caller ...
- vue大型电商项目尚品汇(后台篇)day02
这几天更新有点小慢,逐渐开始回归状态了.尽快把这个后台做完,要开始vue3了 3.添加修改品牌 用到组件 Dialog 对话框,其中visible.sync这个配置是修改他的显示隐藏的,label-w ...
- 想写个小说,关于C#的,名字就叫《原Csharp》吧 (第一回 买书未成炁自生 惶惶回屋遇老翁)
以前也有写过一些小说,但是总是写写停停的,因为忙于项目和其他事情,总是耽搁很久(真的是很久)才会继续动两笔,所以我想先在这里以随笔的方式写个关于C#异世界的小故事吧,更新随缘,也稍微能让自己轻松些. ...
- redis相关知识点
redis 的相关知识点 启动 启动代码 redis-cli -a 密码 通用命令 expire: 设置有效期 expire name 10 key key * 相关数据类型 String set:添 ...
- VR技术的应用领域有哪些?
5G时代已经到来,新技术的商用不仅能够为用户带来更快的网络连接速度,那些依靠网速的提升,而得以更快发展的科技企业,也迎来了新的发展契机. 这其中,尤为值得关注的是VR领域. 谈起VR,这并不算一个陌生 ...
- crane:字典项与关联数据处理的新思路
前言 在我们日常开发中,经常会遇到一些烦人的数据关联和转换问题,比如典型的: 对象属性中个有字典 id,需要获取对应字典值并填充到对象中: 对象属性中有个外键,需要关联查询对应的数据库表实体,并获取其 ...
- Cayley 定理与扩展 Cayley 定理
Cayley 定理 节点个数为 \(n\) 的无根标号树的个数为 \(n^{n−2}\) . 这个结论在很多计数类题目中出现,要证明它首先需要了解 \(\text{Prufer}\) 序列的相关内容. ...
- 记一次 .NET 某工控数据采集平台 线程数 爆高分析
一:背景 1. 讲故事 前几天有位朋友在 B站 加到我,说他的程序出现了 线程数 爆高的问题,让我帮忙看一下怎么回事,截图如下: 说来也奇怪,这些天碰到了好几起关于线程数无缘无故的爆高,不过那几个问题 ...
- 链表设计与Java实现,手写LinkedList这也太清楚了吧!!!
链表设计与实现 在谈链表之前,我们先谈谈我们平常编程会遇到的很常见的一个问题.如果在编程的时候,某个变量在后续编程中仍需使用,我们可以用一个局部变量来保存该值,除此之外一个更加常用的方法就是使用容器了 ...
- 洛谷 P1714 切蛋糕 单调队列
这个题比较显然,要用前缀和来做.但只用前缀和是过不去的,会TLE,所以需要进行优化. 对于每个前缀和数组 b 中的元素,都可以找到以 b[i] 结尾的子段最大值 p[i],显然,最终的 ans 就是 ...