Python 集合(列表 ,元组,集合, 字典)

一Python 集合

Python 编程语言中有四种集合数据类型:

列表(List)是一种有序和可更改的集合。允许重复的成员。
元组(Tuple)是一种有序且不可更改的集合。允许重复的成员。
集合(Set)是一个无序和无索引的集合。没有重复的成员。
字典(Dictionary)是一个无序,可变和有索引的集合。没有重复的成员。

二.列表(List)

列表是一个有序且可更改的集合。在 Python 中,列表用方括号编写。

实例
1.创建列表:

thislist = ["apple", "banana", "cherry"]
print(thislist)

2.访问项目
您可以通过引用索引号来访问列表项:

实例
打印列表的第二项:

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

3.负的索引
负索引表示从末尾开始,-1 表示最后一个项目,-2 表示倒数第二个项目,依此类推。

实例
打印列表的最后一项:

thislist = ["apple", "banana", "cherry"]
print(thislist[-1])

索引范围
您可以通过指定范围的起点和终点来指定索引范围。
指定范围后,返回值将是包含指定项目的新列表。

实例
返回第三、第四、第五项:

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])

注释:搜索将从索引 2(包括)开始,到索引 5(不包括)结束。
请记住,第一项的索引为 0。

负索引的范围
如果要从列表末尾开始搜索,请指定负索引:

实例
此例将返回从索引 -4(包括)到索引 -1(排除)的项目:

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-4:-1])

4.更改项目
如需更改特定项目的值,请引用索引号:

实例
更改第二项:

thislist = ["apple", "banana", "cherry"]
thislist[1] = "mango"
print(thislist)

5.遍历列表
您可以使用 for 循环遍历列表项:

实例
逐个打印列表中的所有项目:

thislist = ["apple", "banana", "cherry"]
for x in thislist:
  print(x)

您将在 Python For 循环 这一章中学习有关 for 循环的更多知识。

6.检查项目是否存在
如需确定列表中是否存在指定的项,请使用 in 关键字:

实例
检查列表中是否存在 “apple”:

thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
  print("Yes, 'apple' is in the fruits list")

7.列表长度
如需确定列表中有多少项,请使用 len() 方法:

实例
打印列表中的项目数:

thislist = ["apple", "banana", "cherry"]
print(len(thislist))

8.添加项目
如需将项目添加到列表的末尾,请使用 append() 方法:

实例
使用 append() 方法追加项目:

thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)

9.要在指定的索引处添加项目,请使用 insert() 方法:

实例
插入项目作为第二个位置:

thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)

10.删除项目
有几种方法可以从列表中删除项目:

实例
remove() 方法删除指定的项目:

thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)

实例
pop() 方法删除指定的索引(如果未指定索引,则删除最后一项):

thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)

实例
del 关键字删除指定的索引:

thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)

实例
del 关键字也能完整地删除列表:

thislist = ["apple", "banana", "cherry"]
del thislist

实例
clear() 方法清空列表:

thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)

11.复制列表
您只能通过键入 list2 = list1 来复制列表,因为:list2 将只是对 list1 的引用,list1 中所做的更改也将自动在 list2 中进行。

有一些方法可以进行复制,一种方法是使用内置的 List 方法 copy()。

实例
使用 copy() 方法来复制列表:

thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)

制作副本的另一种方法是使用内建的方法 list()。

实例
使用 list() 方法复制列表:

thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)

12.合并两个列表
在 Python 中,有几种方法可以连接或串联两个或多个列表。
最简单的方法之一是使用 + 运算符。

实例
合并两个列表:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]

list3 = list1 + list2
print(list3)

连接两个列表的另一种方法是将 list2 中的所有项一个接一个地追加到 list1 中:

实例
把 list2 追加到 list1 中:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]

for x in list2:
  list1.append(x)

print(list1)

或者,您可以使用 extend() 方法,其目的是将一个列表中的元素添加到另一列表中:

实例
使用 extend() 方法将 list2 添加到 list1 的末尾:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]

list1.extend(list2)
print(list1)

13.list() 构造函数
也可以使用 list() 构造函数创建一个新列表。

实例
使用 list() 构造函数创建列表:

thislist = list(("apple", "banana", "cherry")) # 请注意双括号
print(thislist)

三.元组(Tuple)

元组是有序且不可更改的集合。在 Python 中,元组是用圆括号编写的。

实例
1.创建元组:

thistuple = ("apple", "banana", "cherry")
print(thistuple)

2.访问元组项目
您可以通过引用方括号内的索引号来访问元组项目:

实例
打印元组中的第二个项目:

thistuple = ("apple", "banana", "cherry")
print(thistuple[1])

3.负索引
负索引表示从末尾开始,-1 表示最后一个项目,-2 表示倒数第二个项目,依此类推。

实例
打印元组的最后一个项目:

thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])

索引范围
您可以通过指定范围的起点和终点来指定索引范围。
指定范围后,返回值将是带有指定项目的新元组。

实例
返回第三、第四、第五个项目:

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5])

注释:搜索将从索引 2(包括)开始,到索引 5(不包括)结束。请记住,第一项的索引为 0。
负索引范围
如果要从元组的末尾开始搜索,请指定负索引:

实例
此例将返回从索引 -4(包括)到索引 -1(排除)的项目:

thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[-4:-1])

4.更改元组值
创建元组后,您将无法更改其值。元组是不可变的,或者也称为恒定的。

但是有一种解决方法。您可以将元组转换为列表,更改列表,然后将列表转换回元组。

实例
把元组转换为列表即可进行更改:

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x)

5.遍历元组
您可以使用 for 循环遍历元组项目。

实例
遍历项目并打印值:

thistuple = ("apple", "banana", "cherry")
for x in thistuple:
  print(x)

6.检查项目是否存在
要确定元组中是否存在指定的项,请使用 in 关键字:

实例
检查元组中是否存在 “apple”:

thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
  print("Yes, 'apple' is in the fruits tuple")

7.元组长度
要确定元组有多少项,请使用 len() 方法:

实例
打印元组中的项目数量:

thistuple = ("apple", "banana", "cherry")
print(len(thistuple))

8.添加项目
元组一旦创建,您就无法向其添加项目。元组是不可改变的。

实例
您无法向元组添加项目:

thistuple = ("apple", "banana", "cherry")
thistuple[3] = "orange" # 会引发错误
print(thistuple)

创建有一个项目的元组
如需创建仅包含一个项目的元组,您必须在该项目后添加一个逗号,否则 Python 无法将变量识别为元组。

实例
单项元组,别忘了逗号:

thistuple = ("apple",)
print(type(thistuple))

#不是元组
thistuple = ("apple")
print(type(thistuple))

9.删除项目
注释:您无法删除元组中的项目。

元组是不可更改的,因此您无法从中删除项目,但您可以完全删除元组:

实例
del 关键字可以完全删除元组:

thistuple = ("apple", "banana", "cherry")
del thistuple

print(thistuple) # 这会引发错误,因为元组已不存在

10.合并两个元组
如需连接两个或多个元组,您可以使用 + 运算符:

实例
合并这个元组:

tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3)

11.tuple() 构造函数
也可以使用 tuple() 构造函数来创建元组。

实例
使用 tuple() 方法来创建元组:

thistuple = tuple(("apple", "banana", "cherry")) # 请注意双括号
print(thistuple)

四.集合(Set)

集合是无序和无索引的集合。在 Python 中,集合用花括号编写。

实例
1.创建集合:

thisset = {"apple", "banana", "cherry"}
print(thisset)

注释:集合是无序的,因此您无法确定项目的显示顺序。

2.访问项目
您无法通过引用索引来访问 set 中的项目,因为 set 是无序的,项目没有索引。

但是您可以使用 for 循环遍历 set 项目,或者使用 in 关键字查询集合中是否存在指定值。

实例
遍历集合,并打印值:

thisset = {"apple", "banana", "cherry"}

for x in thisset:
  print(x)

实例
检查 set 中是否存在 “banana”:

thisset = {"apple", "banana", "cherry"}

print("banana" in thisset)

3.更改项目
集合一旦创建,您就无法更改项目,但是您可以添加新项目。

4.添加项目
要将一个项添加到集合,请使用 add() 方法。
要向集合中添加多个项目,请使用 update() 方法。

实例
使用 add() 方法向 set 添加项目:

thisset = {"apple", "banana", "cherry"}

thisset.add("orange")

print(thisset)

实例
使用 update() 方法将多个项添加到集合中:

thisset = {"apple", "banana", "cherry"}

thisset.update(["orange", "mango", "grapes"])

print(thisset)

5.获取 Set 的长度
要确定集合中有多少项,请使用 len() 方法。

实例
获取集合中的项目数:

thisset = {"apple", "banana", "cherry"}

print(len(thisset))

6.删除项目
要删除集合中的项目,请使用 remove() 或 discard() 方法。

实例
使用 remove() 方法来删除 “banana”:

thisset = {"apple", "banana", "cherry"}

thisset.remove("banana")

print(thisset)

注释:如果要删除的项目不存在,则 remove() 将引发错误。

实例
使用 discard() 方法来删除 “banana”:

thisset = {"apple", "banana", "cherry"}

thisset.discard("banana")

print(thisset)

注释:如果要删除的项目不存在,则 discard() 不会引发错误。

您还可以使用 pop() 方法删除项目,但此方法将删除最后一项。请记住,set 是无序的,因此您不会知道被删除的是什么项目。

pop() 方法的返回值是被删除的项目。

实例
使用 pop() 方法删除最后一项:

thisset = {"apple", "banana", "cherry"}

x = thisset.pop()

print(x)

print(thisset)

注释:集合是无序的,因此在使用 pop() 方法时,您不会知道删除的是哪个项目。

实例
clear() 方法清空集合:

thisset = {"apple", "banana", "cherry"}

thisset.clear()

print(thisset)

实例
del 彻底删除集合:

thisset = {"apple", "banana", "cherry"}

del thisset

print(thisset)

7.合并两个集合
在 Python 中,有几种方法可以连接两个或多个集合。

您可以使用 union() 方法返回包含两个集合中所有项目的新集合,也可以使用 update() 方法将一个集合中的所有项目插入另一个集合中:

实例
union() 方法返回一个新集合,其中包含两个集合中的所有项目:

set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

set3 = set1.union(set2)
print(set3)

实例
update() 方法将 set2 中的项目插入 set1 中:

set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

set1.update(set2)
print(set1)

8.set() 构造函数
也可以使用 set() 构造函数来创建集合。

实例

thisset = set(("apple", "banana", "cherry")) # 请留意这个双括号
print(thisset)

五.字典(Dictionary)

字典是一个无序、可变和有索引的集合。在 Python 中,字典用花括号编写,拥有键和值。

实例
1.创建并打印字典:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
print(thisdict)

2.访问项目
您可以通过在方括号内引用其键名来访问字典的项目:

实例
获取 “model” 键的值:

x = thisdict["model"]

还有一个名为 get() 的方法会给你相同的结果:

实例
获取 “model” 键的值:

x = thisdict.get("model")

3.更改值
您可以通过引用其键名来更改特定项的值:

实例
把 “year” 改为 2019:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict["year"] = 2019

4.遍历字典
您可以使用 for 循环遍历字典。

循环遍历字典时,返回值是字典的键,但也有返回值的方法。

实例
逐个打印字典中的所有键名:

for x in thisdict:
  print(x)

实例
逐个打印字典中的所有值:

for x in thisdict:
  print(thisdict[x])

实例
您还可以使用 values() 函数返回字典的值:

for x in thisdict.values():
  print(x)

实例
通过使用 items() 函数遍历键和值:

for x, y in thisdict.items():
  print(x, y)

5.检查键是否存在
要确定字典中是否存在指定的键,请使用 in 关键字:

实例
检查字典中是否存在 “model”:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
if "model" in thisdict:
  print("Yes, 'model' is one of the keys in the thisdict dictionary")

6.字典长度
要确定字典有多少项目(键值对),请使用 len() 方法。

实例
打印字典中的项目数:

print(len(thisdict))

7.添加项目
通过使用新的索引键并为其赋值,可以将项目添加到字典中:

实例

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict["color"] = "red"
print(thisdict)

8.删除项目
有几种方法可以从字典中删除项目:

实例
pop() 方法删除具有指定键名的项:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict.pop("model")
print(thisdict)

实例
popitem() 方法删除最后插入的项目(在 3.7 之前的版本中,删除随机项目):

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict.popitem()
print(thisdict)

实例
del 关键字删除具有指定键名的项目:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
del thisdict["model"]
print(thisdict)

实例
del 关键字也可以完全删除字典:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
del thisdict

print(thisdict) #this 会导致错误,因为 "thisdict" 不再存在。

实例
clear() 关键字清空字典:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
thisdict.clear()
print(thisdict)

9.复制字典
您不能通过键入 dict2 = dict1 来复制字典,因为:dict2 只是对 dict1 的引用,而 dict1 中的更改也将自动在 dict2 中进行。

有一些方法可以进行复制,一种方法是使用内建的字典方法 copy()。

实例
使用 copy() 方法来复制字典:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
mydict = thisdict.copy()
print(mydict)

制作副本的另一种方法是使用内建方法 dict()。

实例
使用 dict() 方法创建字典的副本:

thisdict =	{
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
mydict = dict(thisdict)
print(mydict)

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年11月23日
下一篇 2023年11月23日

相关推荐