dictionary in python

黄舟
Release: 2017-01-19 17:35:46
Original
1611 people have browsed it

Dictionary: an associative array or hash table, an object indexable by keyword.
Purpose of dictionary: Define an object that can contain multiple named fields, and can also be used as a container to quickly find unordered data
Dictionaries are the most complete data type in Python and are most commonly used for storage and processing in programs. Data
How to create:
1, put the value in {} to create an empty dictionary;
2, use the method dict() to create an empty dictionary

data = {  
     "name" : "神行太保戴宗",  
     'title' :'天速星',  
     'age' : 45,  
     'price' : 490  
}
Copy after login

To access Dictionary members use the keyword index operator s[name]:

name = data['name'];  
title = data['title'];  
age = data['age'];  
print(name);  
print(title);  
print(age);
Copy after login

The output result is:

神行太保戴宗
天速星
45
Copy after login

Methods to insert or modify objects:

data['book'] = '水浒传之梁山108将'; #插入  
data['name'] = '插翅虎雷横';  #修改  
data['title'] = '天退星';
Copy after login

Output result:

水浒传之梁山108将
插翅虎雷横
天退星
Copy after login

String is a commonly used keyword type
Find unordered data:

prices = {  
 'apple' :3.4,  
 'banana' : 4,  
 'orange' : 2.5,  
 'lemon' : 3.7,  
  'pear' : 1.8  
}
Copy after login
applePrice = prices['apple'];
Copy after login

Output result:

3.4
Copy after login


How to determine whether an item is a member of the current dictionary:
1, use the in operator to test whether a content item is a member of the dictionary

if "grape" in prices:  
  p = prices['grape'];  
else:  
  p= 0;  
print(p);
Copy after login

Output result:

0
Copy after login
Copy after login


2, use the system method get to determine whether it is a dictionary member

p = prices.get('grape',0);  
  
print(p);
Copy after login

Output result:

0
Copy after login
Copy after login


Get the dictionary key The list of words only needs to convert the dictionary into a list:

pricelist = list(prices);
Copy after login

Output result:

['orange', 'lemon', 'pear', 'banana', 'apple']
Copy after login

Method to delete dictionary elements del:

del prices['pear'];
Copy after login

Output result:

{'apple': 3.4, 'banana': 4, 'lemon': 3.7, 'orange': 2.5}
Copy after login

Summary:
1. What is the dict dictionary? : is an associative array or hash table
2, create dictionary: 1, {} 2, dict()
2, purpose of dictionary: used to quickly find unordered data and often used to store and process data
3, use dictionary keyword index to obtain data
4, dictionary insertion and modification: use keyword index to add or modify the format s[name] = 'data';
5, determine whether the element exists in the dictionary : 1, in 2, get
6, method to get dictionary keywords: list is declared as list
6, deletes elements in the dictionary: del method

The above is in python Dictionary content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!