In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is analogy in python". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what analogy is in python.
The so-called analogy
Why do you say that? Because of the teaching mode of these tutorials, it is not easy for readers to find the similarities of strings, lists, and tuples.
Let's look at these three data structures from the perspective of "reading". Suppose there is a string called a, a list called b, and a tuple called c. Then please take a look at the following operation.
(1) read elements by index
A [0] # string first character b [0] # list first element c [0] # tuple first element a [3] # string 4th character b [3] # list 4th element c [3] # fourth element a [- 1] # string last character b [- 1] # list last tuple The last element of prime c [- 1] # tuple
The operation of reading elements by index makes strings, lists, and tuples exactly the same.
(2) slice
A [1: 4] # read string 2, 3, 4 characters b [1: 4] # read list 2, 3, 4 elements c [1: 4] # read tuple 2, 3, 4 elements a [: 4] # read the first 4 characters of the string b [: 4] # read the first 4 elements of the list c [: 4] # read the first 4 elements of the tuple a [- 3:] # read the last 3 characters of the string b [- 3:] # read the last 3 elements of the list c [- 3:] # read the last 3 elements of the tuple
In the operation of slicing, strings, lists, and tuples are exactly the same.
(3) cyclic iteration
For char in a: # iterate over strings print (char) for element in b: # iterate over lists print (element) for element in c: # iterate over tuples print (element)
Iterate through this operation in a loop, with strings, lists, and tuples exactly the same.
(4) output in reverse order
A [:-1] # reverse the strings b [:-1] # reverse the list c [:-1] # reverse the tuples
The running effect is shown in the following figure:
Output this operation in reverse order, with strings, lists, and tuples exactly the same.
(5) whether the element is inside or not
If'x'in a: print ('character x is in the string!') If 3 in b: print ('element 3 is in the list') If 3 in c: print ('element 3 is in tuple!')
Determine whether a character is in a string, or whether an element is in a list or tuple, the operation is exactly the same.
(6) find the index
A = 'hello' index = a.index (' e') print (the index of the character e in the string is:', index) b = ['kingname',' pm', 'ui',' spider'] index = b.index ('pm') print (' element pm is indexed in the list as:', index) c = ('kingname',' pm', 'ui') 'spider') index = c.index (' pm') print ('the index of element pm in the tuple is:', index)
The running effect is shown in the following figure:
Query the index of a substring in a string, or query its index in a list or tuple based on an element, the operation is exactly the same.
(7) count the number of occurrences
A = 'helloworld' num = a.count (' l') print ('character l appears in the string is:', num) b = ['kingname',' pm', 'ui',' pm', 'spider'] num = b.count (' pm') print ('element pm appears in the list is:', num) c = ('kingname',' pm', 'ui',' pm') 'spider') num = c.count (' pm') print ('the number of times the element pm appears in the tuple is:', num)
The running effect is shown in the following figure:
Count the number of occurrences, which is exactly the same for strings, lists, and tuples.
(8) splicing operation
A = 'hello' b =' world' print (a + b) a = [1,2,3] b = [4,5,6] print (a + b) a = (1,2,3) b = (4,5,6) print (a + b)
The running effect is shown in the following figure:
In the operation of concatenating plus signs, strings, lists, and tuples are exactly the same.
Summary in the "read" operation, strings, lists, tuples are exactly the same, as long as you master one of them, the other two automatically know how to operate. It's called analogy.
At this point, I believe you have a deeper understanding of "what is analogy in python". 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.