容器类型

2018-11-13  本文已影响0人  L丶Y1122

1.列表的相关方法

1.列表赋值
# 现象1:
list1 = [1, 2, 3]
list2 = list1
list2.append(100)
print(list1)   # [1, 2, 3, 100]
list1 = [1, 2, 3]
list2.append(200)
print(list1)

# 现象2:
list1 = [1, 2, 3]
list2 = list1[:]
list2.append(100)
print(list1)   # [1, 2, 3]
2.列表中的方法
numbers = [100, 34, 90, 89, 100, 7, 100, 18]
print(numbers.count(100))
numbers.extend(['abc', 'hello'])
print(numbers)
numbers.extend('world')
print(numbers)
numbers.extend(range(11, 15))
print(numbers)

注意:
a.如果元素有多个,只去第一个的下标
b.如果这个元素不存在,会报错(ValueError)

numbers = [3, 1, 2, 3, 4, 5, 3]
print(numbers.index(3))   # 0
# print(numbers.index(33))  # ValueError: 33 is not in list
numbers = [19, 89, 2, 8, 98, 10, 32]
numbers.reverse()
print(numbers)
numbers = [19, 89, 2, 8, 98, 10, 32]
numbers.sort()
print(numbers)

numbers = [19, 89, 2, 8, 98, 10, 32]
numbers.sort(reverse=True)
print(numbers)

names = ['路飞', '娜美', '山治', '罗宾', '弗兰克', '乔巴', '佐罗']
names.sort()
print(names)
names = ['路飞', '娜美', '山治', '罗宾', '弗兰克', '乔巴', '佐罗']
names.clear()
print(names)

# 注意: 清空列表尽量使用clear()
names = ['路飞', '娜美', '山治', '罗宾', '弗兰克', '乔巴', '佐罗']
names = []
print(names)
list1 = [1, 2, 3]
list2 = list1.copy()
print(list2)
print(list1 is list2)   # False

2.浅拷贝和深拷贝

1.copy.copy(对象) - 浅拷贝 (直接拷贝元素的值产生一个新的地址)
2.copy.deepcopy(对象) - 深拷贝(不会直接复制地址,而是将地址对应的值拷贝一份产生新的地址)
 import copy
numbers1 = [1, 2, 3]
numbers2 = [10, 20, 30]
list1 = [numbers1, numbers2]

print('1.浅拷贝:')
list2 = list1.copy()
print('修改前list1:', list1)
print('修改前list2:', list2)
print('针对list1进行修改')
list1.append(111)
list1[0].append(100)
print('修改后list1:', list1)
print('修改后list2:', list2)

numbers1 = [1, 2, 3]
numbers2 = [10, 20, 30]
list1 = [numbers1, numbers2]
print('2.深拷贝')
list2 = copy.deepcopy(list1)
print('修改前list1:', list1)
print('修改前list2:', list2)
print('针对list1进行修改')
list1.append(111)
list1[0].append(100)
print('修改后list1:', list1)
print('修改后list2:', list2)

3.元组

1.什么是元祖(tuple)

元祖就是不可变的列表。(有序,不可变)
有序 - 可以通过下标获取元素
不可变 - 不支持增、删、改

2.元祖的字面量: 通过小括号将多个元素括起来,多个元素之间用逗号隔开

tuple1 = (1, True, 'abc', [1, 2], 1)
print(tuple1)

tuple2 = (10,)
print(tuple2, type(tuple2))
tuple3 = 1, 2, 3, 'abc'
print(tuple3, type(tuple3))
tuple4 = (10, 20)
print(tuple4[0], tuple4[-2])
# 可以通过变量个数和元祖元素个数保持一致来获取元祖中的每个元素
x, y = tuple4  # x, y = 10, 20
print(x, y)

# 通过在变量名前加*,获取没有*的变量获取到的元素的剩下的部分。以列表的形式返回
tuple5 = ('余婷', 98, 90, 99, 87, 78)
name, *scores = tuple5
print(name, scores)  # '余婷' [98, 90, 99, 87, 78]

name, number, *scores = tuple5
print(name, number)   # '余婷'  98
print(scores)  # [90, 99, 87, 78]
*list1, num = tuple5
print(list1, num)  # ['余婷', 98, 90, 99, 87]  78

num1, *list1, num2 = tuple5
print(num1, num2)   # 余婷 78
print(list1)  # [98, 90, 99, 87]
# (了解)
tuple1 = (1, 2, 3)
print(*tuple1)
list1 = ['aa', 'bb', 'cc']
# a = list1[0]
# b = list1[1]
# c = list1[2]
# *list1 == a b c
print(*list1)
3.获取元祖元素和列表获取列表回去列表元素一模一样
tuple1 = 1, 2, 3, 4, 5
print(tuple1[1])
print(tuple1[:3])

for item in tuple1:
    print(item)
4.相关运算和列表一样
print((1, 2) + ('a', 'n'))
print((1, 2) * 3)
print(1 in (1, 2))
print(len((1, 2, 4)))
print(max((1, 89, 0)))
print(min((1, 89, 0)))
5.元祖相关的方法: 只有列表中的count和index

4.字典

1.什么是字典(dict)

字典是python中内置的容器类的数据类型,可变,无序的。字典的元素是键值对

2.字典的字面量:使用大括号括起来,大括号中是键值对,多个键值对之间用逗号隔开
dict1 = {'aa': 100, 10: 'abc', (10, 20): 'hello'}

# 列表和字典不能作为key
# dict2 = {{'a': 1}: 100, 10: 'abc', (10, 20): 'hello'}  # TypeError

# key是唯一的
dict2 = {'aa': 100, 'aa': 200, 'bb': 300}
print(dict2)   # {'aa': 200, 'bb': 300}

什么时候使用字典:
如果一个容器里面存储的数据是不同意义的数据(数据之间需要区分),就使用字典

# 用一个变量来存储一个学生的信息:姓名、年龄、电话、成绩、学号
student = ['小明', 28, '1627399992', 30, '1982001', 98]
print(student[0])

student = {'name': '小明', 'age': 28, 'tel': '1627399992', 
'score': 30, '学号':'1982001'}
print(student['name'])

5.字典的增删改查

1.查(获取字典的值)
dog1 = {'name': '旺财', 'age': 3, 'color': '黄色', 'type': '土狗'}
print(dog1['name'])
print(dog1['color'])
# print(dog1['sex'])   # KeyError: 'sex'

print(dog1.get('age'))
print(dog1.get('type'))
print(dog1.get('sex'))
# 直接遍历字典拿到的是字典中所有的key
for key in dog1:
    print(key, dog1[key])

# 同时获取key和value(看着方便但是性能差,内存消耗多)
print(dog1.items())
for key, value in dog1.items():
    print(key, value)
2.增(添加键值对)

字典[key] = 值 - 当key不存的时候,就是在字典中添加键值对

dict1 = {'a': 100}
dict1['b'] = 200
print(dict1)
dict1 = {'a': 100, 'b': 200}
# 当key值有重名的时候,会用序列中键值对对应的值,更新原字典的key对应的值
dict1.update({'aa': 10, 'bb': 20, 'a': 'abc'})
print(dict1)

dict1.update([[1, 2], ['a', 2], [2, 'b']])
print(dict1)

dict1.update([('aaa', 100), [12, 200]])
print(dict1)
3.改(修改key对应的值)

字典[key] = 值 - 当key存在的时候,就是修改key对应的值

dict1 = {'a': 10, 'b': 20}
dict1['a'] = 100
print(dict1)
4.删(删除键值对)
person = {'name': '张三', 'age': 30, 'sex': '男'}
del person['sex']
print(person)
person = {'name': '张三', 'age': 30, 'sex': '男'}
age = person.pop('age')
print(person, age)
person = {'name': '张三', 'age': 30, 'sex': '男'}
# 删除最后一个键值对(取出最后一个键值对, 以元祖的形式返回) - 无意义
value = person.popitem()
print(person, value)

6.字典的相关运算

1. in 和 not in

key in 字典 - 判断字典中是否存在指定的key

dict1 = {'a': 12, 'b': 20, 'c': 30}
print('a' in dict1)
上一篇 下一篇

猜你喜欢

热点阅读