Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of dictionaries in Python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "What is the use of dictionaries in Python". The content is simple and easy to understand and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "What is the use of dictionaries in Python".

1. Concept of dictionary

In Python, the dictionary data type is called "dict" in English, and in some languages it is called "hash". Dictionaries are one of the most commonly used data structures in programming. It is used to map or store key pairs you need, so that when you need them, you can get their value by key. Similarly, programmers don't use a term like "dictionary" to refer to things that don't work like a real dictionary full of words, so we just use it like a dictionary in the real world.

Difference between dictionary and list

For lists you can do things like this:

>>> things = ['a', 'b', 'c', 'd']

>>> print things[1]

b >>> things[1] =

'

z'

>>> print things[1]

z >>> things

['a', 'z', 'c', 'd']

You can use numbers as indexes to lists, meaning you can find elements in lists by numbers. You should now understand these properties of lists, and you should also understand that you can only access the elements of lists by numbers. And dict is something that allows you to find elements by anything, not just numbers. Yes, dictionaries can relate one object to another, regardless of their type. Let's see:

>>> stuff = {'name': 'Zed', 'age': 39, 'height': 6 * 12 + 2}

>>> print stuff['name']

Zed

>>> print stuff['age']

39

>>> print stuff['height']

74

>>> stuff['city'] = "San Francisco"

>>> print stuff['city']

San Francisco

3. Use of Dictionaries

In addition to numbers, we can also use strings to get stuff from dictionaries, and we can also use strings to add elements to dictionaries. Of course, it supports not only strings, but also things like this:

>>> stuff[1] = "Wow"

>>> stuff[2] = "Neato"

>>> print stuff[1]

Wow

>>> print stuff[2]

Neato

>>> stuff

{'city': 'San Francisco', 2: 'Neato', 'name': 'Zed', 1: 'Wow', 'age': 39, 'height': 74

}

In this code, numbers are used. When printing stuff, there are not only numbers but also strings as dictionary keys. Of course, a dictionary that can only put things in it is meaningless, so we also have to delete it, which is to use the keyword del:

>>> del stuff['city']

>>> del stuff[1]

>>> del stuff[2]

>>> stuff

{'name': 'Zed', 'age': 36, 'height': 74}

4. Example exercises

# create a mapping of state to abbreviation

states = {

'Oregon': 'OR',

'Florida': 'FL',

'California': 'CA',

'New York': 'NY',

'Michigan': 'MI'

}

# create a basic set of states and some cities in them

cities = {

'CA': 'San Francisco',

'MI': 'Detroit',

'FL': 'Jacksonville'

}

# add some more cities

cities['NY'] = 'New York'

cities['OR'] = 'Portland'

# print out some cities

print '-' * 10

print "NY State has: ", cities['NY']

print "OR State has: ", cities['OR']

# print some states

print '-' * 10

print "Michigan's abbreviation is: ", states['Michigan']

print "Florida's abbreviation is: ", states['Florida']

# do it by using the state then cities dict

print '-' * 10

print "Michigan has: ", cities[states['Michigan']]

print "Florida has: ", cities[states['Florida']]

# print every state abbreviation

print '-' * 10

for state, abbrev in states.items():

print "%s is abbreviated %s" % (state, abbrev)

# print every city in state

print '-' * 10

for abbrev, city in cities.items():

print "%s has the city %s" % (abbrev, city)

# now do both at the same time

print '-' * 10

for state, abbrev in states.items():

print "%s state is abbreviated %s and has city %s" % (

state, abbrev, cities[abbrev])

print '-' * 10

# safely get a abbreviation by state that might not be there

state = states.get('Texas')

if not state:

print "Sorry, no Texas. "

# get a city with a default value

city = cities.get('TX', 'Does Not Exist')

print "The city for the state 'TX' is: %s" % city

The results you see:

$ python ex39.py

----------

NY State has: New York

OR State has: Portland

----------

Michigan's abbreviation is: MI

Florida's abbreviation is: FL

----------

Michigan has: Detroit

Florida has: Jacksonville

----------

California is abbreviated CA

Michigan is abbreviated MI

New York is abbreviated NY

Florida is abbreviated FL

Oregon is abbreviated OR

----------

FL has the city Jacksonville

CA has the city San Francisco

MI has the city Detroit

OR has the city Portland

NY has the city New York

----------

California state is abbreviated CA and has city San Francisco

Michigan state is abbreviated MI and has city Detroit

New York state is abbreviated NY and has city New York

Florida state is abbreviated FL and has city Jacksonville

Oregon state is abbreviated OR and has city Portland

----------

Sorry, no Texas.

The city for the state 'TX' is: Does Not Exist

The above is all the content of this article "What is the use of dictionaries in Python?" Thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report