Python-day7-集合和字典

1,官网的数据类类型介绍:https://docs.python.org/zh-cn/3/library/stdtypes.html#mapping-types-dict

2,Runoob的集合介绍:https://www.runoob.com/python3/python3-set.html#setmethod

3,Runoob的字典介绍:https://www.runoob.com/python3/python3-dictionary.html

 

#生成字典测试,以及集合运算测试

 

 

# 生成一个长字典。Generate a dict
import random

dictNameAge = {}
nameList =[]
ageList = []
for i in range(10000):
nameLength = random.randint(2, 8)
nameChars = random.sample('zyxwvutsrqponmlkjihgfedcba',nameLength)
name = ''.join(nameChars)
name = name.capitalize()
nameList.append(name)
age = random.randint(2, 120)
ageList.append(age)

print("名字列表%d列表%d" % (len(nameList), len(ageList)))

for i in range(0, len(nameList)):
dictNameAge[nameList[i]] = ageList[i]

print(len(dictNameAge))
print("这个长字典", len(dictNameAge), dictNameAge)

# operate on the dict

for k, v in dictNameAge.items():
if ("mt" in k != -1) or (v == 55):
print(k, v)

print("Qc在字典里", "Yc" in dictNameAge)

# 集合的生成和

setupASet = set(nameList)
print("集合", len(setupASet))

list1 = nameList[0:len(nameList)//2]
list2 = nameList[len(nameList)//2:]
set1 = set(nameList[0:len(nameList)//2])
set2 = set(nameList[len(nameList)//2:])

print(len(list1), len(set1), len(list2), len(set2))
print("两个集合的度和", len(set1)+len(set2))

unionSet = set()

print("两个集合并集之后的度和", len(unionSet.union(set1, set2)))

# intersectionSet = set()
print("两个集合交集之后的度和", len(set1.intersection(set2)))

print("两个集合差集之后的度和", len(set1.difference(set2)))

# a = frozenset([1, 2, 3, 4])
# b = set([4,5,6])




 

posted @ 2019-07-22 16:31  Augustone  阅读(241)  评论(0编辑  收藏  举报