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 are the redundant codes that are often written in python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Python is often written out of redundant code what, for this problem, this article details the corresponding analysis and solution, hoping to help more want to solve this problem of small partners to find a simpler and easier way.

Some of you migrated to Python from other languages, so you write Python code with some syntax features from other languages. The Python code you write contains a lot of useless code. Let's take a look at some common invalid codes today.

Define before copy

Lists and dictionaries are the hardest hit. Some people create a list of elements in Python that are already determined, but write code like this:

a = list() a.append('x') a.append('y') a.append('z')

But in fact, you can just write one line of code:

a = ['x', 'y', 'z']

Initialize a dictionary whose elements have already been determined. Some people like to write code like this:

a = dict() a['name'] = 'kingname' a['age'] = 100 a['address'] = 'xx'

But in fact, one line of code can do it:

a = {'name': 'kingname', 'age': 100, 'address': 'xx'}

There are also some people whose dictionary Key is stored in the list, which is written like this:

key_list = [] for key in target_dict.keys(): key_list.append(key)

But in fact, one line of code does it:

key_list = list(target_dict) Condition to determine whether true, false, or empty

Some people write conditional judgments like this:

if a == False: print('xx') if a == []: print('xx') if a == '': print('xx') if a == None: print('xx') if a == 0: print('xx')

But in fact, all these judgments can be combined into one:

if not a: print('xx')

Similarly, for true, non-null, non-zero codes:

if a == True if len(a) > 0 if a != 0 if a != None

All can be combined into:

if a: print ('xxx ') Slightly technical redundancy

Some people write conditional branch detection like this:

for ele in target_list: if ele > 0: print ('At least one number in the list is greater than 0') return True else: continue

The else: continue here doesn't actually have any effect and can be deleted altogether:

for ele in target_list: if ele > 0: print ('At least one number in the list is greater than 0') return True

A syntactic sugar we often use. But in Python, there is actually a generator push, which is written as (x for x in yyy), where parentheses are used. So for the following lines of code:

a = ['1', '2', '3'] sum_result = sum([int(x) for x in a])

A student who knows the generator-push formula might write something like:

a = ['1', '2', '3'] sum_result = sum((int(x) for x in a))

But in practice, if one generator pushes the formula as the only argument to another function, then the inner parenthesis can be omitted:

a = ['1',' 2','3'] sum_result = sum(int(x) for x in a) The answers to the questions that are often written in Python are shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts, you can pay attention to the industry information channel to learn more.

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