关于python3.x:Python入门系列四别再傻傻分不清列表元组字典集合的区别

2次阅读

共计 7173 个字符,预计需要花费 18 分钟才能阅读完成。

四句话总结

  • 列表是一个有序且可更改的汇合,容许反复成员。
  • 元组是一个有序且不可更改的汇合,容许反复成员。
  • 汇合是一个无序、不可更改 * 且未索引的汇合,没有反复成员。
  • 字典是一个有序且可更改的汇合,没有反复成员。

私有的局部

获取长度,应用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

正文完
 0