## test for dict
print(‘==========start test dict==========‘)
names = [‘Aa‘, ‘Bb‘, ‘Cc‘]
scores = [98, 76, 54]
## d相当于做了一个map映射
d = {‘Aa‘: 98, ‘Bb‘: 76, ‘Cc‘: 54}
print(‘d is‘,d)
for name in names:
print(‘%s is‘ % name,d[name] )
## get可在查询不到数据时不返回错误,返回指定数值或字符串
print(‘Xy not exist, result of Xy is‘,d.get(‘Xy‘,-1) )
## 可通in判断查询数据是否存在于dict
i = 0
names.append(‘Xy‘)
while i < 10 :
if not names[i] in d:
print(‘break , names[%d] not exist‘ % i,names[i])
break
print(‘names[%d] is‘ % i,names[i])
print(‘d[%s] is‘ % names[i],d[names[i]])
i = i + 1
print(‘d is‘,d)
##删除映射可用pop
d.pop(‘Aa‘)
print(‘after pop,d is‘,d)
print(‘=========start test set==========‘)
##set是一组字符的集合,但是数据自动去重,可用add和remove增加和删除元素,但是依然会去重复
s = set([‘a‘, ‘b‘, ‘c‘,‘a‘])
print(‘s is‘,s)
s.add(‘a‘)
s.add(‘d‘)
print(‘after s.add , s is‘,s)
s.remove(‘d‘)
print(‘after s.remove , s is‘,s)
s2 = set([‘a‘, ‘b‘, ‘e‘])
##set可做&和|等操作
print(‘s2 is‘,s2)
print(‘s & s2 is‘,s & s2)
print(‘s | s2 is‘,s | s2)