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 difference between a Python dictionary and a regular dictionary

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

Share

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

This article mainly explains "what is the difference between Python dictionary and regular dictionary". The explanation in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "what is the difference between Python dictionary and regular dictionary" together!

An example illustrates the difference between a sorted dictionary and a regular dictionary:

import HTMLParser import urllib import sys #Define HTML parser class parseLinks(HTMLParser.HTMLParser): def handle_starttag(self, tag, attrs): if tag == 'a': for name,value in attrs: if name == 'href': print value print self.get_starttag_text() #Create an instance of HTML parser lParser = parseLinks() #Open HTML file lParser.feed(urllib.urlopen( \ "http://www.python.org/index.html").read()) lParser.close()

As you can see, ordered dictionaries maintain an original order of elements, whereas standard dictionaries do not. One caveat, though, is if you populate the dictionary with named parameters rather than key/value pairs. It maintains its order. Maybe it's a bug.

Because using named parameters is an ideal way to initialize dictionaries, and the elements have a definite left-to-right order. The elements we're using here are exactly the same as the *** examples: this class can access its contents using a typical set of dictionary methods, keys(), values(), and items();

However, the update() method of this class differs from the update() method of a regular dictionary. It can accept sequences or maps with integer values. If you are using a sequence. It counts the number of elements and adds them to the original number. For a map, it counts each object in the map and adds the result to the original count. The following code updates the Counter class initialized in the example above:

import HTMLParser import urllib import sys #Define HTML parser class parseLinks(HTMLParser.HTMLParser): def handle_starttag(self, tag, attrs): if tag == 'a': for name,value in attrs: if name == 'href': print value print self.get_starttag_text() #Create an instance of HTML parser lParser = parseLinks() #Open HTML file lParser.feed(urllib.urlopen( \ "http://www.python.org/index.html").read()) lParser.close()

The Itertools module can handle not only infinite sequences but also finite sequences. In Python applications, it introduces two new functions: the combinations_with_replacement() function and the compress() function.

Thank you for reading, the above is "Python dictionary and conventional dictionary is what the difference is" content, after the study of this article, I believe that we have a deeper understanding of the difference between Python dictionary and conventional dictionary, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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