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

How to make python code more Pythonic

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

Share

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

Today, I will talk to you about how to make the python code more Pythonic. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

This article, mainly introduces a few simple techniques, let you write Python code, more Pythonic.

Variable exchange

Pythonic writing method

Common writing method

Loop through interval elements

In Python2, there are range and xrange2 writing methods, and xrange is a generator writing method, which saves memory. Range in Python3 is equivalent to xrange in Python2.

The generator is generated dynamically only when it is used, and can only be used once, such as range (1000000). A list of 1 million elements is generated in Python2, but not a list, but a generator in Python3, which takes up very little memory.

If you are still using Python2, it is recommended to use xrange instead of range

The way Java is written

Generator extension

Indexes

Pythonic writing method

Common writing method

Obviously, Pythonic is written in a more intuitive and elegant way.

String concatenation

Pythonic writing method

Common writing method

Each + operation produces a new string, resulting in a waste of memory, while join produces only one string object in the whole process.

File opening and closing

Pythonic writing method

Common writing method

Using with,Python will automatically manage the opening and closing of file streams without manual operation

List operation

Pythonic writing method

Common writing method

List can also delete * elements with pop (0), but list is stored sequentially in memory. Deleting * elements will cause all subsequent elements to move forward, which is inefficient and similar to insertion.

Avoid using list if there are a large number of deletions and inserts at the beginning

Deconstruction assignment

Pythonic writing method

Similarly, in Python2, the dictionary's items method returns a list, which consumes a lot of memory when the dictionary is large. The iteritems method returns the generator.

In Python3, no iteritems,items is equivalent to the iteritems of Python2.

If you are using Python2, use iteritems instead of items

Derivation type

Pythonic writing method

Common writing method

After reading the above, do you have any further understanding of how to make python code more Pythonic? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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: 252

*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