Python Training Knowledge Summary Series-chapter II Python data structure first part, list and for loop

Source: Internet
Author: User

List and looping issues

    1. Write a function tag_count whose parameters are listed as a list of strings. The function should return the number of XML tags in the string. XML is a data language similar to HTML. You can determine whether a string is an XML tag, starting with the left angle bracket "<" and ending with the closing angle bracket ">".

You can assume that the list of strings as input does not contain an empty string.

"" "Write a function, tag_count that's takes as its argument a list
of strings. It should return a count of how many of those strings
is XML tags. can tell if a string was an XML tag if it begins
With a left angle bracket "<" and ends with a right angle bracket ">".
"""
#TODO: Define the Tag_count function
def tag_count (list):
Count=0
For each in list:
A= ",". Join (Each.title ())
Print (a)
If a[0]== ' < ' and a[-1]== ' > ':
Count=count+1
return count
List1 = [' <greeting> ', ' Hello world! ', ' </greeting> ']
Count = Tag_count (List1)
Print ("Expected result:2, Actual result: {}". Format (count))

I'm turning the list into strings, strings with "," split, and then judging if it's the first and last one is <,>

Standard answer: "Python
def tag_count (tokens):
Count = 0
For tokens in tokens:
If token[0] = = ' < ' and token[-1] = = ' > ':
Count + = 1
return count

I use string indexing to find out if each token begins and ends with angle brackets.

Python Training Knowledge Summary Series-chapter II Python data structure first part, list and for loop

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.