程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python dictionary

編輯:Python

Dictionaries

Dictionary use {} Express

There are two things in the dictionary. The first is key You can understand this thing as an index , When we have this key When , You don't need an index

# Define a dictionary
a = {"name" : "wangyangzheng","age" : 18,"school" : "asthu"}
# key :name, age,school
# value :wangyangzheng,age,asthu
# visit
print(a ["name"])
# Remember to add ""
# If it is not added, it will happen name Undefined errors

Dictionaries are out of order , If we judge whether two dictionaries are equal , The order of dictionary elements is different , But the elements are the same , It's the same

a = {'1' : 1, '2' : 2}
b = {'2' : 2, '1' : 1}
print(a == b)
# Output True

How to operate the dictionary

  • keys()
    We go through .keys() Returns all the keys in the dictionary
spam = {'size' : 'fat','color' : 'gray','disposition' : 'loud'}
# Return a list like this
print(spam.keys())
# Output one by one spam Key
for i in spam.keys():
print(i)
  • values()
    .values() Returns all values of the dictionary
spam = {'size' : 'fat','color' : 'gray' ,'disposition' : 'loud'}
print(spam.values())
# Output the values of the dictionary one by one
for i in spam.values():
print(i)
  • items()
    .items() Returns the key value pair of the dictionary , Similar types are returned
spam = {'size' : 'fat','color' : 'gray' ,'disposition' : 'loud'}
print(spam.items())
# Output key value pairs one by one
for i in spam.items():
print(i)

Does the dictionary have a value

If this value exists, it is in, If not, it's not int

spam = {'size' : 'fat','color' : 'gray','disposition' : 'loud'}
print('name' in spam.keys()) # return False Express 'name' No spam Key
print('gray' in spam.values())# return True Express 'gray' yes spam Value
print('gray' not in spam.keys())# return False Express 'gray' No spam Key

get() and setdefault Method

  • get()
    get() Method has two parameters , The first parameter is the key corresponding to the value to be taken , The second parameter is the alternate value to be returned ( If this key doesn't exist )
spam = {'apple' : 5,'cups' : 2}
print("I'm bring" + str(spam.get('cups',0)) + 'cups' ) # 2
print("I'm bring" + str(spam.get('pen',0)) + 'pens') # Output standby value 0
  • setdefault()
    setdefault() Method also has two parameters , The first parameter is the key corresponding to the value to be taken , The second parameter is the spare value

If this key doesn't exist , Then put both keys and values into the dictionary

spam = {'1':1}
print(spam.setdefault('1',0))
print(spam.setdefault('2',2))

We find that the output value of the third line is 2

This is because ’2’ This key doesn't exist ,setdefault Method adds both the key and the value

We can verify it

print(spam)

It was found that it was


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved