#字典的一键多值 print ' scheme a list as the value value of Dict allows the repetition of ' d1={} key=1 value=2 D1.setdefault (key,[]). Append (value) value=2 D1.setdefau LT (key,[]). Append (value) print D1 #获取值 print ' scheme one gets the value ' Print List (D1[key]) ' print ' scheme a delete value, leaving an empty list ' D1[key].remov E (value) d1[key].remove (value) Print D1 print ' Scheme one checks if there is a value ' Print D1.get (key,[]) print ' scenario two using a sub-dictionary as a value value for Dict not allowed to repeat ' d1={} key=1 keyin=2 value=11 d1.setdefault (key,{}) [Keyin]=value keyin=2 value=22 D1.setdefault (key,{}) [Keyin]=value ke Yin=3 value=33 D1.setdefault (key,{}) [Keyin]=value print D1 print ' Scenario two get value ' Print List (D1[key]) ' print ' scenario two delete value, will leave An empty list ' del D1[key][keyin] keyin=2 del d1[key][keyin] Print d1 print ' Scenario Two check if there is a value ' Print D1.get (key, ()) print ' Scenario three Using Set as Dict value value does not allow duplicate ' d1={} key=1 value=2 D1.setdefault (Key,set ()). Add (value) value=2 D1.setdefault (Key,set ()). Add ( Value) value=3 D1.setdefault (Key,set ()). Add (value) print D1print ' Scenario three get value ' Print List (D1[key]) print ' Scenario three delete value, leaving an empty list ' D1[key].remove (value) value=2 d1[key].remove (value) print D1 print ' Scheme three checks if there is a value ' Print D1.get (key, ())
Python dictionary with multiple values in one key