In some small python applications, when you do not need a relational database, you can easily use a persistent dictionary to store name-value pairs, which is very similar to the python dictionary, the main difference is data... in some small python applications, you can easi
Search and delete of dictionary dictionaryHow dictionaries are created:# Create Dictionary Type info = { ' name ': ' Li Bai ', ' age ': ' + ' , ' sex ': ' male '}msg = { ' User01 ': ' Longzeluola ', ' user02 ': ' Xiaozemaliya ', ' user03 ': ' Cangjingkong '}When a dictionary stores data, it takes the form of Key:value, which is a set of unordere
Dictionary values can be any python object without restrictions. They can be either standard objects or user-defined objects, but cannot be keys. Two important points must be remembered: 1) the same key cannot appear twice. If the same key is assigned twice during creation, the last value will be remembered> dict1 = {'ob1': 'computer ', 'ob2': 'mouse ', 'ob1': 'printer '} >>>
contains an entry with a key of KDescription
Key type: The key of a dictionary is not necessarily an integral type, or it may be another immutable type
Auto Add: Even if the key start does not exist in the dictionary, you can assign a value to it so that the dictionary creates a new item and cannot associate the value to an index outside the list range
Membershi
{' B ': 2} #清空字典 >>> dict1.clear () >>> dict1 #字典变为空了 {} #删除字典对象 >>> del dict1 >>> dict1 Traceback (most recent call last): Accessing values in the dictionaryPut the corresponding key into square brackets, as follows:Dict = {' name ': ' Zara ', ' age ': 7, ' Class ': ' First '} print ("dict[' name ']:", dict[' name ']) print ("dict[' age ']:", dict [' Age '])#以上实例输出结果:Dict[' Name ']: Zaradict['
In Python, metadata groups, lists, Dictionary differences, and python dictionaries
Python has three built-in data structures: List, tuples, and dictionary.1. ListList is the data structure that processes a group of ordered projects. You can store a series project in a list.
': 'Earth ', 'port': 80}
>>>
>>>> For key in dict2.keys ():
... Print 'key = % s, value = % s' % (key, dict2 [key])
...
Key = name, value = earth
Key = port, value = 80
② Starting from Python 2.2
Traverse the dictionary in the for loop.
>>> Dict2 = {'name': 'Earth ', 'port': 80}
>>>
>>>> For key in dict2:
... Print 'k
operatorsAs with strings, you can use the + and * numbers to perform operations between tuples. This means that they can be combined and copied, and a new tuple is generated after the operation.7.5, tuple index InterceptL = (' spam ', ' spam ', ' spam! '); Print l[2]; # ' spam! ' Print L[-2]; # ' Spam ' Print l[1:]; #[' Spam ', ' spam! ']7.6, tuple built-in fun
I. Creating a dictionary
Method ①:
>>> Dict1 = {}
>>> dict2 = {' name ': ' Earth ', ' Port ': 80}
>>> Dict1, Dict2
({}, {' Port ': +, ' name ': ' Earth '})
Method ②: From Python version 2.2
>>> fdict = Dict ([' X ', 1], [' Y ', 2])
>>> fdict
{' Y ': 2, ' X ': 1}
Method ③:
From the Python 2.3 version, you can create a "default"
List, String, Dictionary common operations in python, python string
The list operation is as follows:
A = ["haha", "xixi", "baba"]Increment: a. append [gg]A. insert [1, gg] To the place marked as 1, add ggDelete: a. remove (haha) deletes the first matched haha from left to right in the list.Del a. [0] Delete the value corresponding to 0 as the subscriptA. NO cont
Example of how to use the python command line youdao dictionary, python command line
Preface
Since I have been using the Linux system, it is particularly difficult to support dictionaries. For those of me who have been reading English documents, I have been stuck in shells and used a proper dictionary before, I feel ve
Pythondictionary (Dictionary)A dictionary is another mutable container model and can store any type of object.Each key value of the dictionary is key=>value with a colon : Split, with a comma, split between each key-value pair, and the entire dictionary is enclosed in curly braces {}, as shown in the following format:
(v) , when K does not exist, it executes d[k] = [] and returns the empty list, which then joins v into the list.
The value passed into the Defualtdict constructor is not necessarily a class, it can also be a callable function, and when the corresponding key is not in the defualtdict, its default value is the return value of the function, for example:
>>> from collections import defaultdict
>>> def zero_default (): return
0
>>> d = Defaultdict (zero_default)
>>> d[' a '] = 1
>>> d['
A dictionary is the only built-in ing type in python. The values in the dictionary have no special order, but they are all stored in a specific key. A dictionary is the only built-in ing type in python. The values in the dictionary
simulationUser LogonThe data system is used as an example to describe how to use the dictionary.
#!/usr/bin/env python db = {} def newuser(): prompt = 'register: ' while True: name = raw_input(prompt) if db.has_key(name): prompt = 'name taken, try another: ' continue else: break pwd = raw_input('passwd: ') db[name] = pwd def olduser(): name = raw_input('login: ') pwd = raw_input('passwd: ') passwd = db.g
() method is used to create a new dictionary, passing in two parameters, sequences, and initial values. Http://www.runoob.com/python/att-dictionary-fromkeys.htmlCollections's counter module, see the next articleIv. Ordering of dictionariesProblem Introduction:The Python dictionary
refers to the constant . That point ' a ', it cannot be changed to point to ' B ', pointing to a list, cannot be changed to point to other objects, but the list itself is variable! To create a tuple that does not change content, you must ensure that each element of the tuple itself cannot be changed. List and tuple are Python's built-in ordered set, one variable, one immutable. Choose to use them as needed.The two commonly used methods of a tuple are: count () counts the number of elements of a
']:", dict ['school'];# Output result of the above instance:# Dict ['age']: 8# Dict ['school ']: DPS School
4. delete dictionary elementsOnly one operation is required to delete a single element and clear the dictionary.
The following example shows how to delete a dictionary using the del command:
The code is as follows:
#! /Usr/bin/pythonDict = {'name': 'Zar
Data structures that refer to values by name in Python are called mappings (mapping). Dictionaries are the only types of mappings in Python that are built in (the Python interpreter itself supports, and does not require import). The values in the dictionary are not in a special order and are stored under a specific key
The dictionary in Python is a data structure of Python, which is essentially a collection of key and value and its corresponding relationship, and a key can correspond to more than one value. Reasonable use of the dictionary can give us a lot of programming convenience.-----data types in
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.