鏈表:dict
dict = {key: value}
變量 = set(list); ---> s = set([1, 2, 4])
註:dict、set的key必須是不可變對象。
區別:1、dict有value,set沒有
2、set可以做交集&,並集| 運算,dict 不能
一、dirct
#!/usr/bin/python
#initialize
dict = {'you':95, 'love':20, 'me':5}; # 若存在dict = {'you', 'love':, :8} 格式是錯誤的,key:value要配齊
print dict['love'];
#add a key
dict['too'] = 1;
print dict;
#delete a key, its value also delete
dict.pop('love');
print dict;
#a way of check in dict
print 'you' in dict; #格式:key in dict;
#other way of check in dict
print dict.get('haha'); #當key不存在,默認返回None ,若存在返回value
print dict.get('you', 1);
二、set
#!/usr/bin/python
#initialize
s = set([1, 2, 3, 4, 4]); #key重復會自動合並
print s;
#add
s.add(5);
print s;
#delete
s.remove(4);
print s;
# & / |
s1 = set([1, 2, 3]);
s2 = set ([2, 4, 5]);
s3 = s1 & s2;
print s3;
print s1 | s2;
list = ['y', 'o', 'u', 'l', 'o'];
q = set(list);
print q;
tuple = ('1', '2', '3');
x = set(tuple);
print x;
#error way
tuple = ('1', '2', '3');
x = set(tuple);
print x;
可見set並不支持混合的key。
鏈表:dict