字典与集合

一、字典(dictionary)

字典与列表类似,也是可变序列;与列表不同的是,列表是有序的,而字典是无序的,所以字典不可以通过索引(下标)读取。字典中的内容是以“键--值”的形式保存的,即key-value。其中,键(key)是唯一的,而值(value)可以相同。

1.字典的创建和删除:

key和value用冒号分隔,字典使用大括号。格式为:

dictionary={'key1':'value1','key2':'value2',...'keyn':'valuen',}  

还可通过dict()函数创建字典。例如:dictionary=dict(zip(list1,list2))  

其中zip()将多个列表对应位置组成对,list1最后变成字典的key,list2列表最后变成字典的value。

删除字典用del ,若只想清空字典的全部元素,可使用dictionary.clear() 

字典的主要特征如下:

1.通过key而不是索引(index)来读取元素:字典有时也称为关联数组或者散列表(hash)

2.字典是任意对象的无序集合:字典各项是从左到右随机排序的。

3.字典是可变的,并且可以任意嵌套(即字典的value可以是列表或者其他字典)。

4.字典的key必须唯一且不可变。

二、集合(set)

因为集合中没有重复的元素,故集合的主要作用就是去重。

1.创建集合:一种是直接使用 { } 创建;另一种是通过set()函数将列表、元组等可迭代对象转换为集合。

注:创建空集合时,只能使用set(),不能使用 { } 实现,因为直接使用 { } 表示创建一个空字典。

2.添加、删除元素:setname.add() 表示向集合中添加元素;setname.pop()、setname.remove()表示移除集合中的元素。

3.集合的交集、并集和差集运算:“ & ”交集运算;“ | ”并集运算;“ - ”差集运算。

 

 

 

 

字典
定义:
使用键-值(key-value)存储,具有极快的查找速度
注意: 字典是无序的

key的特性:
1、字典中的key必须唯一
2、key必须是不可变对象
3、字符串、整数等都是不可变的,可以作为key
4、list是可变的,不能作为key

思考: 保存多位学生的姓名与成绩
使用字典,学生姓名为key,学生成绩作为值

      dict1 = {"tom":60, "lilei":70}

元素的访问
获取: 字典名[key]

print(dict1["lilei"])
#print(dict1["sunck"])#没有
print(dict1.get("sunck"))
ret = dict1.get("sunck")
if ret == None:
    print("没有")
else:
    print("有")
#输出:
70
None
没有

添加

dict1["hanmeimei"] = 99

修改

#因为一个key对应一个value,所以,多次对一个key的value赋值,其实就是修改值
dict1["lilei"] = 80
print(dict1)

删除

dict1.pop("tom")
print(dict1)

遍历

dict1 = {"tom":60, "lilei":70}
for key in dict1:
    print(key, dict1[key])
#输出:
#tom 60
#lilei 70

print(dict1.values())     #输出:dict_values([60, 70])
for value in dict1.values(): #[60,80,90]
    print(value)
#输出:60     70

print(dict1.items())   #输出:dict_items([('tom', 60), ('lilei', 70)])
for k, v in dict1.items():
    print(k, v)
#输出:
#tom 60
#lilei 70

for i, v2 in enumerate(dict1):
    print(i, v2)
#输出:
0 tom
1 lilei

字典和list比较
1、查找和插入的速度极快,不会随着key-value的增加而变慢
2、需要占用大量的内存,内存浪费多
list
1、查找和插入的速度随着数据量的增多而减慢
2、占用空间小,浪费内存少

w = input()
#w = "good"

str = "sunck is a good man!sunck is a nice man!sunck is a hands man! sunck is a good man!sunck is a nice man!sunck is a great man!sunck is a noble man!sunck is a cool man!"

print(str.count(w))

集合(set)
概述: 类似dict,是一组key的集合,不存储value
本质: 无序和无重复元素的集合
创建
创建set需要一个list或者tuple或者dict作为输入集合,重复元素在set中会自动被过滤

s1 = set([1,2,3,4,5,3,4,5])
print(s1)
s2 = set((1,2,3,3,2,1))
print(s2)
s3 = set({1:"good", 2:"nice"})
print(s3)
#输出:
#{1, 2, 3, 4, 5}
#{1, 2, 3}
#{1, 2}

添加

s4 = set([1,2,3,4,5])
s4.add(6)
s4.add(3) #可以添加重复的,但是不会有效果
#s4.add([7,8,9]) #set的元素不能是列表,因为列表是可变的
s4.add((7,8,9))
#s4.add({1:"a"}) #set的元素不能是字典,因为字典是可变的
print(s4)
#输出:{1, 2, 3, 4, 5, 6, (7, 8, 9)}

update(): 插入整个list、tuple、字符串,打碎插入

s5 = set([1,2,3,4,5])
s5.update([6,7,8])
s5.update((9,10))
s5.update("sunck")
print(s5)
#输出:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'n', 'c', 'u', 's', 'k'}

删除

s6 = set([1,2,3,4,5])
s6.remove(3)
print(s6)

遍历

s7 = set([1,2,3,4,5])
for i in s7:
    print(i)

set没有索引

print(s7[3])    #报错

enumerate(): 用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中

for index, data in enumerate(s7):
    print(index, data)
#输出:
0 1
1 2
2 3
3 4
4 5

交集与并集

s8 = set([1,2,3])
s9 = set([2,3,4])
#交集
a1 = s8 & s9
print(a1)
print(type(a1))
#并集
a2 = s8 | s9
print(a2)
print(type(a2))

类型转化

#list-->set
l1 = [1,2,3,4,5,3,4,5]
s1 = set(l1)

#tuple-->set
t2 = (1,2,3,4,3,2)
s2 = set(t2)

#set-->list
s3 = {1,2,3,4}
l3 = list(s3)
print(l3)

#set-->tuple
s4 = {2,3,4,5}
t4 = tuple(s4)
print(t4)

 

用set去重

l = [1,2,3,4,3,4,5,6]
'''
s = set(l)
l = list(s)
print(l)
'''
l = list(set(l))
print(l)
posted @ 2020-12-06 15:46  世俗-  阅读(658)  评论(0)    收藏  举报