Python dictionary multiple

Source: Internet
Author: User

#encoding =utf-8

print ' China '

#字典的一键多值

print ' Scheme A list is allowed to repeat as a value of Dict '

d1={}
Key=1
value=2
D1.setdefault (key,[]). Append (value)
value=2
D1.setdefault (key,[]). Append (value)

Print D1

#获取值
print ' Scheme one get value '
Print List (D1[key])

print ' scheme deletes a value, leaving an empty list '
D1[key].remove (value)
D1[key].remove (value)
Print D1

print ' Scenario One checks if there is a value '
Print D1.get (key,[])

print ' scenario two using a sub-dictionary as a value value for Dict does not allow duplicates '

d1={}
Key=1
keyin=2
value=11
D1.setdefault (key,{}) [Keyin]=value
keyin=2
Value=22
D1.setdefault (key,{}) [Keyin]=value
Keyin=3
Value=33
D1.setdefault (key,{}) [Keyin]=value

Print D1

print ' Scenario two get value '
Print List (D1[key])

print ' Scenario Two delete value, leaving 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 duplicates '
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 D1

print ' Scheme 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 check if there is a value '
Print D1.get (key, ())

Python dictionary multiple

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.