【Python】Python3.7.3 - Collections (Arrays) - List数据类型

版权声明:转载必须保留原出处,没有书面许可不可用于商用目的, https://blog.csdn.net/qq_43401808/article/details/89846644

数据类型:List, Tuple, Set, Dictionary

Python有四种不同的数据类型都可以存储数组数据,分别是:

  • List,列表:元素有序且可变,允许重复元素
  • Tuple,元组:元素有序但是不可变,允许重复元素
  • Set,集合:元素无序,无索引,不允许重复元素。
    注:既然无索引,也就无法标识并改变某个元素
  • Dictionary,字典:元素无序,可以索引,可以改变,不允许重复元素

类型声明

List类型使用[]声明,[]内可以放置任何类型的数据,Tuple类型使用()声明,Set与Dictionary都使用{}进行声明,但是Set与Dictionary声明的格式不同:Set保存元素的结合,而Dictionary则主要用于关联数组。这四种数据类型可以保存任何类型的元素,不仅限于字符串。

类型 声明
List color_list = [ "red", "blue", "green"]
Tuple color_tuple = ("red", "blue", "green")
Set color_set = {"red", "blue", "green"}
Dictionary car_dict = {"brand": "Audi", "model": "A8", "year": 2019}
# define some variables
color_list = ["red", "blue", "green"]
color_tuple = ("red", "blue", "green")
color_set = {"red", "blue", "green"}
car_dict = {"brand": "Audi", "model": "A8", "year": 2019}

# print the types of each variable
print(type(color_list))
print(type(color_tuple))
print(type(color_set))
print(type(car_dict))

# print each variable
print(color_list)
print(color_tuple)
print(color_set)
print(car_dict)
--- Console Output --- 
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>

['red', 'blue', 'green']
('red', 'blue', 'green')
{'green', 'red', 'blue'}
{'brand': 'Audi', 'model': 'A8', 'year': 2019}

List 常用操作

List是有序可变数组,可以执行的常用的操作包括:

  • 索引操作(返回值,改变值)
  • 排序,反转操作
  • 遍历操作
  • 插入,追加与扩展操作
  • 删除操作(按索引,按内容)
  • 清除所有元素操作
color_list = ["red", "blue", "green"]
color_tuple = ("red", "blue", "green")
color_set = {"red", "blue", "green"}
car_dict = {"brand": "Audi", "model": "A8", "year": 2019}

# index into an element
print("first element of color_list: " + color_list[0])
print("last element of color_list: " + color_list[-1])
print("the length of color_list:" + str(len(color_list)))

# change the first element
color_list[0] = "redder"
print("first element of color_list: " + color_list[0])

# iterate each element
for color in color_list:
    print(color)

# range(0,3) only generates [0,1,2] (3 not included)
for index in range(0, len(color_list)):
    print(str(index) + " : " + color_list[index])

print("# 打印指定元素计数个数")
print(color_list.count("redder"))

print("# 复制一个list")
color_list2 = color_list.copy()
print(color_list2)

print("# 扩展列表")
color_list.extend(color_list2)
print(color_list)

print("# 排序列表")
color_list.sort()
print(color_list)

print("# 反转列表")
color_list.reverse()
print(color_list)

print("# 追加重复元素: 'black'")
color_list.append("black")
color_list.append("black")
print(color_list)

print("# 插入一个元素:把新元素插入到指定的索引上。")
color_list.insert(1, "Magenta")
print(color_list)

print("# 按索引删除第一个元素")
del color_list[0]
print(color_list)

print("# 按索引删除第一个元素,同时返回删除的值")
print(color_list.pop(0))
print(color_list)

print("# 删除指定的元素:只删除找到的第一个元素 black")
color_list.remove("black")
print(color_list)

color_list.remove("black")
print(color_list)

print("# 删除不存在的元素,产生ValueError异常")
try:
    color_list.remove("black")
except ValueError:
    print("I caught a ValueError!!!")
else:
    print("remove okay")
finally:
    print(color_list)

print("# 清除所有元素")
color_list.clear()
print(color_list)
--- Console Output --- 
first element of color_list: red
last element of color_list: green
the length of color_list:3
first element of color_list: redder
redder
blue
green
0 : redder
1 : blue
2 : green
# 打印指定元素计数个数
1
# 复制一个list
['redder', 'blue', 'green']
# 扩展列表
['redder', 'blue', 'green', 'redder', 'blue', 'green']
# 排序列表
['blue', 'blue', 'green', 'green', 'redder', 'redder']
# 反转列表
['redder', 'redder', 'green', 'green', 'blue', 'blue']
# 追加重复元素: 'black'
['redder', 'redder', 'green', 'green', 'blue', 'blue', 'black', 'black']
# 插入一个元素:把新元素插入到指定的索引上。
['redder', 'Magenta', 'redder', 'green', 'green', 'blue', 'blue', 'black', 'black']
# 按索引删除第一个元素
['Magenta', 'redder', 'green', 'green', 'blue', 'blue', 'black', 'black']
# 按索引删除第一个元素,同时返回删除的值
Magenta
['redder', 'green', 'green', 'blue', 'blue', 'black', 'black']
# 删除指定的元素:只删除找到的第一个元素 black
['redder', 'green', 'green', 'blue', 'blue', 'black']
['redder', 'green', 'green', 'blue', 'blue']
# 删除不存在的元素,产生ValueError异常
I caught a ValueError!!!
['redder', 'green', 'green', 'blue', 'blue']
# 清除所有元素
[]

猜你喜欢

转载自blog.csdn.net/qq_43401808/article/details/89846644
今日推荐