第七章 字典和集合[DDT书本学习 小甲鱼]【3】

4.copy() 复制字典
a={1:"one",2:"two",3:"three"}
b=a.copy()
print(id(a))
print(id(b))
a[1]="four"
print(a)
print(b)
-------------------
2431102322728
2431102322800
{1: 'four', 2: 'two', 3: 'three'}
{1: 'one', 2: 'two', 3: 'three'}
====================================
5.pop()和popitem() 给定键[弹]出值,后一个[弹]出项
a={1:"one",2:"two",3:"three",4:"four"}
print(a.pop(2))
print(a)
print(a.popitem())
print(a)
-------------------
two
{1: 'one', 3: 'three', 4: 'four'}
(4, 'four')
{1: 'one', 3: 'three'}
======================================
6. setdefault()方法和get()方法相似,区别是get()找不到返回空或指定值
而setdefault()方法在找不到的时候,进行设置添加值。
a={1:"one",2:"two",3:"three",4:"four"}
print(a.setdefault(3))
print(a.setdefault(5))
print(a)
-----------------------
three
None
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: None}
==============================================================
7. update() 更新字典 修改而不增加
pets={1:"龙",2:"虎",3:"猪",4:"狗",5:"鸡",6:"猴"}
pets.update(6="鼠")
print(pets)
------以上代码出现错误,错误原因不可知!!---------更正代码如下
pets={"a":"龙","b":"虎","c":"猪","d":"狗","e":"鸡","f":"猴"}
pets.update(f="鼠")
print(pets)
----------------------------
{'a': '龙', 'b': '虎', 'c': '猪', 'd': '狗', 'e': '鸡', 'f': '鼠'}
=====================================================================
6.2节末尾买下伏笔,函数在收集参数的时候,用到了*,而另外一种是**
def test(*p):
print("有%d个参数"%len(p))
print("第二个参数是:",p[1])
a=[1,2,3,4,5,6,7,8,9]
test(*a) #必须加上*号 进行解包 否则出错
---------------------------------
有9个参数
第二个参数是: 2
================================================
收集参数,有两种打包方式:以元组形式打包,以字典形式打包。
def test(**p):
print("有%d个参数"%len(p))
print("它们分别是:",p)
k={a:1,b:2,c:3,d:4}
test(**k)
-------------------------------
有4个参数
它们分别是: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
===============================================

7.2集合:在我的世界里,你就是【唯一】
上节讲述了字典,在Python里字典对应的是数学里的映射。而今天学习字典的表亲:集合
请看如下代码:
num1={} #默认空字典
print(type(num1))
num2={1,2,3,4,5} #和列表
print(type(num2))
--------------------
<class 'dict'>
<class 'set'>
=====================================集合用来体现唯一特性
举例如下 重复的算一个!!!!!!!
num={1,2,3,4,5,4,3,2,1}
print(num)
------------------------
{1, 2, 3, 4, 5}
============================可以对重复数据进行清理,方便快捷
但是同时要注意,集合时无序的,不能用索引进行引用某一个元素。
num={1,2,3,4,5,4,3,2,1}
print(num[2])
--------- 报错如下 --------------
Traceback (most recent call last):
File "C:/Users/Daodantou/PycharmProjects/s14/day7/t3.py", line 2, in <module>
print(num[2])
TypeError: 'set' object does not support indexing
======================================================================
7.2.1 创建集合 有2种方法 用{}或者用set()方法【字典形式,列表形式】
代码如下
set1={"飞鸟","鱿鱼","豹子","老虎"}
set2=set(["飞鸟","鱿鱼","豹子","老虎"])
print(set1==set2)
---------------------------------------
True
===========================================
以前要求去除列表[1,2,3,4,5,6,3,1,0]种的重复元素,需要这么写:
list1=[1,2,3,4,5,5,3,1,0]
list2=list1[:] #切片操作! 如果list2=list1 则指向同一个列表
list1.clear()
for each in list2:
if each not in list1:
list1.append(each)
print(list1)
-------------------------
[1, 2, 3, 4, 5, 0]
=========================================学习了集合之后,可以这么干=====
list1=[1,2,3,4,5,5,3,1,0]
list1=list(set(list1)) #set()创建的集合 导致了无序 要特别注意
print(list1)
-------------------------
[0, 1, 2, 3, 4, 5]
==============================================================
7.2.2 访问集合
由于集合中的元素是无序的,所以无法用下标来进行访问,可以用迭代方法找元素。
set1={1,2,3,4,5,4,3,2,1,0}
for each in set1:
print(each,end=" ")
-----------------------------在集合建立之初,就自动去除重复了
0 1 2 3 4 5
====================也可以用in和not in判断元素是否存在集合中
set1={1,2,3,4,5,4,3,2,1,0}
print(0 in set1)
print("k" in set1)
print(7 not in set1)
----------------------------------
True
False
True
=======================================
使用add()方法可以为集合添加元素
使用remove()方法可以删除集合中的已知元素
set1={1,2,3,4,5,4,3,2,1,0}
set1.add("wo")
print(set1)
set1.remove(0)
print(set1)
---------------------
{0, 1, 2, 3, 4, 5, 'wo'}
{1, 2, 3, 4, 5, 'wo'}
=================================================================
7.2.3 不可变集合
有时候,我们希望集合中的数据有稳定性,也就是像元组一样不能随意增加或删除元素
那么我们可以定义不可变集合,使用frozenset()函数,没错就是冰冻【frozen】起来:
set1=frozenset({1,2,3,4,5})
set1.add("k")
print(set1)
------------------报错!!!!!!!!!!
Traceback (most recent call last):
File "C:/Users/Daodantou/PycharmProjects/s14/day7/t3.py", line 2, in <module>
set1.add("k")
AttributeError: 'frozenset' object has no attribute 'add'

猜你喜欢

转载自www.cnblogs.com/daodantou/p/10348060.html