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-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the use of the dictionary in python. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.

Dict Python has built-in dictionary: dict support, dict full name dictionary, also known as map in other languages, using key-value * * (key-value) * * storage, with extremely fast search speed.

For example, suppose you want to find the corresponding grade based on the name of the classmate. If you implement it in list, you need two list:

Names = ['Michael',' Bob', 'Tracy'] scores = [95, 75, 85]

Given a name, to find the corresponding score, you must first find the corresponding location in names, and then extract the corresponding score from scores. The longer the list, the longer it takes.

If you implement it in dict, you only need a "name"-"score" comparison table to look up the score directly according to the name. No matter how big the table is, the search speed will not slow down. Write a dict in Python as follows:

D = {'Michael': 95,' Bob': 75, 'Tracy': 85} d [' Michael'] # output 95 Note that KEY in the dictionary is not repeatable hash

If the KEY is duplicated when defining, it overrides the front by default.

I = {"Apple": 50, "Grape": 30, "Apple": 40} print (I) # output {'Apple': 40, 'Grape': 30} add elements to the dictionary

Method: dict ['new_key'] = new_value

A = {'Michael': 95,' Bob': 75, 'Tracy': 85} a [' lee'] = 100print (a) # output {'Michael': 95,' Bob': 75, 'lee':100,Tracy': 85}

Note that the dictionary is unordered, so the location where a ['lee'] is generated is random.

Modify the elements in the dictionary

Method: you can modify the value of value directly. Put a key into value multiple times, and the following value will flush out the previous value:

D = {'Michael': 95,' Bob': 75, 'Tracy': 85} d [' bob'] = 100print (d) # output {'Michael': 95,' Bob': 100, 'Tracy': 85}

If key does not exist, Dict will report an error

> d ['Thomas'] Traceback (most recent call last): File ", line 1, in KeyError:' Thomas' traverses kye,value in dict

Method 1:

A = {"name": "lee", "job": "it", "sex": "man"} for i in a: print (iMagazine a [I]) # efficient # output job it # name lee # sex man

Method 2:

For iLeary n in a.items (): # inefficient print (iMagne) # output content is the same as method 1, but method 1 is more efficient, because a.items () first converts to dictionary list object print (a.items ()) # output content is [('name','lee'), (' job','it'), ('sex','man')]

Some methods of dict dictionary: you can use Python3.5 native commands to find the list of methods and the usage of methods

A = {'name', "lee"} dir (a)

Get the result

# usage, note that there is no need to add () help (a.get)

Get the result

help

Dictionary generation

Used with the built-in function zip ()

Zip (): take the iterable object as a parameter, package the elements in the object into a tuple, and then return a list of these tuples

Method 1: circular method

{iRU j for iJ in zip (keys,values)}

Items = ['apple', "pear", "banana"] prices = [5JEI 3JEI 4] print (zip (items,prices)) print (type (zip (items,prices) # the item price below is a custom variable, and you can replace it at will, such as iGrain1 = {item:price for item,price in zip (items,prices)} print (dict1) # output {'Apple': 5, 'pear': 3, 'banana': 4}

Method 2:dict (zip (keys,values))

Using dict method to convert, less code

Items = ['Apple', 'pear', 'banana'] prices = [5Magne3 items,prices 4] dict2 = dict (zip (items,prices)) print (dict2) # output {'Apple': 5, 'pear': 3, 'banana': 4} above are all the contents of the article "what's the use of dictionaries in python"? thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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