Python数据结构
程序员文章站
2022-04-28 18:38:02
...
Python数据结构
1. List
元素种类可以不一样,如obj_list = ["string", 1, True, 3.14]
如何创建一个空列表?2种方式[ ] 和list()
empty_list = []
empty_list = list()
1.1 insert
weekdays = ['Monday','Tuesday','Wednesday','Thursday','Friday']
weekdays
# 在第一个位置插入“Sunday”
weekdays.insert(0,"Sunday")
weekdays
1.2 delete
- 基于下标删除
del weekdays[0]
weekdays
# 删除第3个之后所有的
al = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
del al[3:]
al
- 基于对象值删除
weekdays.remove('Friday')
weekdays
1.3 append
weekdays = ['Monday','Tuesday','Wednesday','Thursday','Friday']
'Friday' in weekdays
weekdays.append('Friday')
weekdays
weekend = ['Saturday', 'Sunday']
weekdays = weekdays + weekend
weekdays
1.4 pop
- 不加下标移除最后一个
last_day = weekdays.pop()
print("last_day = ", last_day, "\nweekdays = ", weekdays)
- 移除指定位置的
first_day = weekdays.pop(2)
print("first_day = ", first_day, "\nweekdays = ", weekdays)
1.5 sort
- 对于数字,默认升序排列
nums = [1,4,2,5,3]
sorted_nums = sorted(nums)
print("nums =", nums, "\nsorted_nums =", sorted_nums)
nums.sort()
nums
# 降序
nums.sort(reverse=True)
nums
- 对于字符串,默认首字母按字母表顺序排
fruit = ["strawberry","apple","watermelon"]
sorted_fruit = sorted(fruit)
print(sorted_fruit)
1.6 赋值,拷贝
a = [1,2,3]
b = a
c = a.copy()
d = a[:]
e = list(a)
a[0] = 5
print("a = ", a, "\nb = ", b, "\nc = ", c, "\nd = ", d, "\ne = ", e)
1.7 列表的特殊用法
tom_list = ['Tom', 'Male', 20]
a,b,c = tom_list
print(a)
print(b)
print(c)
2. Tuple
2.1 创建元组
- 如何创建一个元组 ()
empty_tuple = ()
week_tuple = ('Monday', 'Tuesday')
week_tuple
(123,456,789)
a = 123,456,789
print(type(a))
print(a[0])
single_tuple = ('Monday',) # single_tuple = ('Monday')
type(single_tuple)
2.2 元组的特殊用法
tom_tuple = ('Tom', 'Male', 20)
name, gender, age = tom_tuple
print("Name = ", name, "\nGender = ", gender, "\nAge = ", age)
2.3 python中交换2个变量
a = 1
b = 2
a, b = b, a
print("a = ", a, "\nb = ", b)
按照其他编程语言的写法
a = 1
b = 2
c = a
a = b
b = c
print(a,b)
2.4 tuple vs list
区别 | tuple | list |
---|---|---|
创建 | () | [] |
修改值 | 不可以 | 可以 |
与字典 | 可以作为字典的关键字 |
3. Dictionary
3.1 创建字典
- 如何创建字典
用 { }
empty_dict = {}
empty_dict
pizza = {
"size":"medium",
"type":"pepperoni",
"crust":"Thick",
"qty": 1,
"deliver":True,
}
pizza
python的key通常为字符串,也可以为任意类型,例如还支持如下创建方式
fruit = {
1:"apple",
(1,1):"cheap",
1.1: "nutritional"
}
print(fruit)
3.2 取值操作
- 取key
pizza.keys()
- 取values
pizza.values()
- 取所有key和value
pizza.items()
- 根据key取value
pizza['type']
# 第2种方式
pizza.get('type')
3.3 添加新的key和value
pizza['topping'] = ['cheese','mushroom']
pizza
3.4 delete,clear
- 删除某行
del pizza['topping']
pizza
- 删除整个元组
pizza.clear()
pizza
4. Set
4.1 创建set
用set()
empty_set = set()
empty_set
集合中不能有重复元素
even_set = {2,4,6,6,8,10}
even_set
输出{2,4,6,8,10}
4.4 集合的一些操作
num_set = {3,6,9,12,15,18}
even_set = {2,4,6,8,10}
# 交
even_set & num_set
# 并
even_set | num_set
# 差
even_set - num_set
num_set - even_set
5. 数据结构的转换
5.1 转List
list('ababc')
输出:['a', 'b', 'a', 'b', 'c']
list((1,2,3,4))
输出:[1, 2, 3, 4]
list({'name': 'Ed', 'employer': 'Oracle'})
输出:['name', 'employer']
list({'name': 'Ed', 'employer': 'Oracle'}.values())
输出:['Ed', 'Oracle']
list({5,6,7,8})
输出:[8, 5, 6, 7]
sorted(list({5,6,7,8}))
输出:[5, 6, 7, 8]
5.2 转tuple
dict('ababc')
输出:报错信息
dict([1,2,3])
输出:报错信息
dict(['ab', 'cd', 'ef'])
输出:{'a': 'b', 'c': 'd', 'e': 'f'}
dict(['abg', 'cd', 'ef'])
输出:报错信息
dict([['a', 'b'], ('c', 'd'), ('e', 'f')])
输出:{'a': 'b', 'c': 'd', 'e': 'f'}
6. zip()
s1 = 'abcdefg'
s2 = 'hijklmn'
list(zip(s1, s2))
输出:[('a', 'h'),
('b', 'i'),
('c', 'j'),
('d', 'k'),
('e', 'l'),
('f', 'm'),
('g', 'n')]
s3, s4 = zip(*d)
print(list(s3))
print(list(s4))
输出:['a', 'b', 'c', 'd', 'e', 'f', 'g'] ['h', 'i', 'j', 'k', 'l', 'm', 'n']
7. 支持修改 vs 不支持修改
s = ["hello"]
print(s)
s[1] = i
输出:报错信息
s = list("hello")
print(s)
s[1]= 'i'
"".join(s)
输出:['h', 'e', 'l', 'l', 'o']
'hillo'
- immutable的数据类型可作为dictionary的索引,tuple可以作为dictionary的索引,list不可以作为dictionary的索引
下一篇: SELECT查询的应用