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

[python learning notes]-01-dict dictionary

編輯:Python

List of articles

  • Preface
  • One 、copy function
  • Two 、 Dictionary members judge
    • in And not in Usage in dictionary &get Judge dictionary value
  • 3、 ... and . Dictionary built-in functions popitem
  • summary


Preface

This section mainly records python Some functions in the dictionary use


Tips : The following is the main body of this article

One 、copy function

Case description : With fruit Take the dictionary describing fruit as an example
The copied new dictionary will not affect the original dictionary

fruits = {

'apple': 30,
'banana': 50,
'pear': 100
}
real_fruits = fruits.copy()
print(real_fruits)
real_fruits['orange'] = 50
real_fruits.update({
'cherry': 100})
print(real_fruits)
real_fruits['apple'] = real_fruits['apple'] - 5
print(real_fruits)
real_fruits['pear'] -= 3
print(real_fruits)
real_fruits.clear()
print(real_fruits)
print(" the second day ")
real_fruits = fruits.copy()
print(real_fruits)

Running results

{
'apple': 30, 'banana': 50, 'pear': 100}
{
'apple': 30, 'banana': 50, 'pear': 100, 'orange': 50, 'cherry': 100}
{
'apple': 25, 'banana': 50, 'pear': 100, 'orange': 50, 'cherry': 100}
{
'apple': 25, 'banana': 50, 'pear': 97, 'orange': 50, 'cherry': 100}
{
}
the second day
{
'apple': 30, 'banana': 50, 'pear': 100}

Two 、 Dictionary members judge

in And not in Usage in dictionary &get Judge dictionary value

Routine usage in and not in

default_dict = {
'a': None, 'b': 1, 'c': 0, 'd': ''}
print('a' in default_dict)
print(bool(default_dict.get('a')))
# get Boolean operations with values 
print(bool(default_dict.get('b')))
print('f' in default_dict)
print('f' not in default_dict)

Running results

True
False
True
False
True

3、 ... and . Dictionary built-in functions popitem

function : Delete the last set of key value pairs in the current dictionary and return
usage : see case
matters needing attention : If the dictionary is empty, an error is reported

# case
students = {
'dewei': ' To ', 'xiaomu': ' stay ', 'xiaoyun': ' Are you there? ', 'xiaogao': ' stay '}
print('xiaogao are you there ')
xiaogao = students.popitem()
print('{} shout {}'.format(xiaogao[0], xiaogao[1]))
print('xiaoyun are you there ')
xiaoyun = students.popitem()
print('{} shout {}'.format(xiaoyun[0], xiaoyun[1]))
print('xiaomu are you there ')
xiaomu = students.popitem()
print('{} shout {}'.format(xiaomu[0], xiaomu[1]))
print('dewei are you there ')
dewei = students.popitem()
print('{} shout {}'.format(dewei[0], dewei[1]))
print(students)

Running results :

xiaogao are you there
xiaogao shout stay
xiaoyun are you there
xiaoyun shout Are you there?
xiaomu are you there
xiaomu shout stay
dewei are you there
dewei shout To
{
}

summary

Tips : Here is a summary of the article :
This section mainly records the common operations of the dictionary


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