Python: Related Operations on dictionaries

Source: Internet
Author: User

>>> people = {"Tom": 175, "Jack": "Kite": "White": "#定义一个字典
>>> for name in people: #获取value的值
Print People[name]

180
160
175
170
>>> for name in people: #获取key的值
Print Name

White
Kite
Jack
Tom
>>> People.keys () # Gets the value of all keys, just like the For loop results above
[' White ', ' Kite ', ' Jack ', ' Tom ']
>>> people.values () # Gets the value of all values, as in the For loop result above
[180, 160, 175, 170]
>>> People.has_key ("white") #判断字典中是否存在该键
True
>>> people["Tom"] #通过key去获取value
170
>>> print [i-I in people if people.get (i) = = 175] #通过value去获取key, a code is done.
[' Jack ']
>>> Print [i * 2 for I in people if people.get (i) = = 175] #让输出的值重复两次
[' Jackjack ']
>>> Print [(i + ") * 2 for I in people if people.get (i) = = 175] #给重复的结果中间插入空格
[' Jack Jack ']
>>> def Test ():
print [i-I in people if people.get (i) = = 175] #把该代码定义成一个函数test ()


>>> Test ()
[' Jack ']
>>> def test01 (SG): #把该代码定义为有参的函数test01 ()
print [i-I in people if people.get (i) = = sg]

>>> test01 (175)
[' Jack ']

Python: Related Operations on dictionaries

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.