List and looping issues
- 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