1. Create a dictionary

describe : Generate Dictionary

grammar :

dic={'k1':'v1'}

Examples :

dic=dict(k1='v1',k2='v2')
dic={'k1':'v1','k2':'v2'}

2. Take key value get

describe : Take key value , If there are no keys in the dictionary , Then output the custom field , The default is none

grammar :

 def get(self, k, d=None): # real signature unknown; restored from __doc__
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
pass

Examples :

dic={'k1':'v1','k2':'v2'}
a=dic.get('k1')
v1
c=dic.get('k3')
none # There is no such key in the dictionary , Show none
c=dic.get('k3','no')
no # There is no such key in the dictionary , Show no

3. Take the value of the key

describe : Take the key in the dictionary

grammar :

 def keys(self): # real signature unknown; restored from __doc__
""" D.keys() -> a set-like object providing a view on D's keys """
pass

Examples :

dic=dict(k1='v1',k2='v2')
dic.keys()
for k in dic.keys():
print(k) result:k1, k2 # Show results

4. Take key value

describe : Take the key value in the dictionary

grammar :

 def values(self): # real signature unknown; restored from __doc__
""" D.values() -> an object providing a view on D's values """
pass

Examples :

dic=dict(k1='v1',k2='v2')
for v in dic.values():
print(v) v=v1,v2 # result

5. Take the value of the key value pair

describe : Take the key value pairs in the dictionary

grammar :

 def items(self): # real signature unknown; restored from __doc__
""" D.items() -> a set-like object providing a view on D's items """
pass

Examples :

dic={'k1':'v1','k2':'v2'}
for k,v in dic.items():
print(k,v) # result
k1 v1
k2 v2

6. Delete key value pair

describe : Delete the key in the dictionary

grammar :

 def pop(self, k, d=None): # real signature unknown; restored from __doc__
"""
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
"""
pass

Examples :

dic=dict(k1='v1',k2='v2',k3='v3')
dic.pop('k2')
print(dic) dic={'k3': 'v3', 'k1': 'v1'} # result

7. Update key value pairs

describe : Update the key in the dictionary

grammar :

 def update(self, E=None, **F): # known special case of dict.update
"""
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]
"""
pass

Examples :

dic=dict(k1='v1',k2='v2',k3='v3')
dic.update({'k4':'v4'})
print(dic) dic={'k4': 'v4', 'k3': 'v3', 'k1': 'v1', 'k2': 'v2'} # result

8. Update key value

describe : Update the key in the dictionary

grammar :

dict[key]=value 

Examples :

dic=dict(k1='v1',k2='v2',k3='v3')
dic['k1']=1
print(dic) dic={'k3': 'v3', 'k1': 1, 'k2': 'v2'} # result

python More related articles on common grammar in dictionary

  1. python The common method in Chinese dictionary

    # Define an empty dictionary : a={ } Define a dictionary : d={'age':18} # Add an element : d['age']=20   d[k]=v d.setdefault('age',18)    d.setde ...

  2. python dict Dictionaries often operate

    Dictionary features :key The only disorder ''' characteristic :key only : disorder ''' info = { 'stu1101': " anhui ", 'stu1102': " Beijing ", 'stu1 ...

  3. python And Dictionaries often operate

  4. 3. Python Dictionaries Summary of common methods

    Python Lexicographers store objects of any type , Such as a string . Numbers . Yuan Zu . list . Dictionaries .bool etc. . advantage : It's easy to take , Fast 1. Create a dictionary The dictionary consists of keys (key) And the corresponding value (value) In pairs . Dictionaries are also called associative arrays or ha ...

  5. Python VS PHP Basic grammar

    I'm studying these days Python, I usually study in order to facilitate memory and better compare and understand the advantages and disadvantages between the two languages in some cases , So it took a little time , Sort it out Python and PHP Some differences in common grammar . One . Case write PHP: ...

  6. Python Learn the introductory Basics (learning Python)--8.3 Dictionary commonly used method function introduction

    The main content of this section is about dict Dictionary of some commonly used methods, the use of functions and examples show . 1. clear Clear dictionary data The syntax is as follows : dict_obj.clear() The sample code is as follows : dict1 = {'web' ...

  7. python MVC、MTV The framework is introduced Django Common syntax of template system

    Django Introduction to the framework .MVC The framework and MTV frame 1.MVC full name Model View Controller, Is a software architecture pattern in software engineering , Divide the software system into three basic parts . advantage : Low coupling High reusability life ...

  8. python Full stack testing -py3 Basics : Basic knowledge of common grammar

    python Basic links python3 Quick reference - python Basics 2 -> if Statement application + while Recycling https://www.cnblogs.com/wuzhiming/p/7795 ...

  9. python Basics -- Dictionaries

    Python Basics -- Dictionaries Common functions of dictionaries : dict.clear( )---> No return value explain : Clear all the elements in the dictionary grammar : In [5]: dict.clear? Type: metho ...

Random recommendation

  1. Use HTML5 Building new support WebRtc Environment as video communication

    It turns out that if you redesign this piece , There will be different ideas . For localizability , We can collect holographic logs , Capture each user asynchronously in the whole system , And then synchronize to a dedicated log analysis system , In this system, according to the user number . Filter the order number ...

  2. Validation of an integer 、 decimal 、 The set of real Numbers 、 Significant decimal is the simplest JavaScript Regular expressions

    After input, press enter to verify !( I think it's the simplest !) Positive integer :   Negtive integer :   whole Count :   A positive fraction :   Negative decimal :   Small Count :   real Count :   Retain 1 Decimal place :  Retain 2 Decimal place :  Retain 3 Decimal place : explain :IE6.0.IE7 ...

  3. [ turn ] The server side of Tianlong Babu Lua Script system

    One .Lua Script function interface 1. LuaInterface.h/.cpp Declare and implement LuaInterface. LuaInterface The members are as follows : // The script engine FoxLuaScriptmLua ; // notes ...

  4. On grunt.js

    package.js { "name": "ttd_v3", "version": "0.1.0", "aut ...

  5. linux install php5.3

    install php The dependency package of [root@localhost admin]# unzip libxml2-2.7.8.tar.zip [root@localhost admin]#tar zvxf libxml ...

  6. android Modify background color ( turn )

    Change to white on black modify AndroidManifest.xml hold android:theme="@style/AppTheme" It is amended as follows android:theme="@andr ...

  7. shallow copy And deep copy

    import copy names = ["zhangyang", 'guyun', 'xiangpeng', ['jack','tom'], 'xuliangchen'] nam ...

  8. Python Using Turing robot to realize wechat chat function

    First of all, you need to go to Turing website to create your own robot, and then get apikey. One . Automatically chat with designated friends # -*- coding: utf-8 -*- """ Created at 2 ...

  9. 2.css How to introduce

    Reference in the web page CSS style inline style Inline style sheets External style sheets .. Linked .. Import type Embedded way style label <!doctype html> <html> <head> ...

  10. mysql in/no in/like

    % Any character _ Any character in (value,......) ad locum not in (value,......) Not here mysql> select 'a' not in (1,2,3, ...