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 use the Python nesting method

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to use Python nesting method". Friends who are interested may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use the Python nesting method.

Title:

Give a string that contains only parentheses (braces "() [] {}"), which can be nested at will. If the same left and right parentheses appear in pairs and the nesting is correct, it is considered to match. For example:

1. ()-> TRUE (match)

2. [()]-> TRUE (matching, parentheses can be nested)

3. () ()-> TRUE (matching, parentheses can be arranged side by side)

4. ({} ([]))-> TRUE (matching, parentheses can be nested arbitrarily, curly braces need not be outside)

5.)-> FALSE (mismatch, missing left parenthesis)

6. (}-> FALSE (does not match, left and right parentheses are not the same)

7. {) (}-> FALSE (mismatch, opposite left and right parentheses)

Train of thought analysis

Since nesting is random, it cannot be peeled from the outermost layer like an onion

No matter how nested it is, there will always be at least a pair of parentheses that do not nest any strings.

When you encounter one, it is impossible to find the corresponding one, because) there are likely to be many, which one corresponds to it? this idea is very troublesome.

There is a kind of data structure called "stack", which is characterized by first in and then out, while list can be disguised as a stack and use the first in and out feature to cancel out the left and right parentheses. The text can no longer be explained clearly, so let's go to the code.

Sample code

# coding=utf-8

Str_value ='({} ([]))'

Lst = list (str_value)

Lst_compare = []

B_format = True

For item in lst:

If item = ='('or item = ='{'or item ='[':

Lst_compare.append (item)

If len (lst_compare) = = 0:

B_format = False

Break

If item =')':

If lst_compare [- 1] ='(':

Lst_compare.pop ()

Else:

B_format = False

If item = =']':

If lst_compare [- 1] ='[':

Lst_compare.pop ()

Else:

B_format = False

If item = ='}':

If lst_compare [- 1] ='{':

Lst_compare.pop ()

Else:

B_format = False

If not len (lst_compare) = = 0:

B_format = False

If b_format:

Print u' format is correct'

Else:

Print u 'format error'

Code parsing

The first thing to understand is the purpose of the lis method used in the second line, which converts the string into a list

Traverse the lst and put all kinds of left parentheses into the st_compare

In the process of traversing the lst, if you encounter a right parenthesis, compare it with the elements at the end of the st_compare to determine whether they are a pair. If they are a pair, the elements at the end of the st_compare are popped up with the pop method. If not, the parentheses do not appear in pairs.

If the length of the final st_compare is not 0, it means that some left parentheses have not been cancelled out, and the format must be incorrect.

At this point, I believe you have a deeper understanding of "how to use the Python nesting method". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report