Python dictionary with multiple values in one key

Source: Internet
Author: User

#字典的一键多值 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.