In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what is the use of lists and tuples in Python. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Basic operation of standard sequence
Sequential index: both forward and reverse indexes can be used. That is, when the index is greater than or equal to 0, the index is from left to right, and when the index is less than 0, from right to left. As shown in the following figure:
Sequence slices:
① can use slices when accessing elements within a specified range, mainly by specifying the range by adding a "colon" to two indexes. However, it is important to note that the first index is contained in the slice, but the second index is not included in the slice. Specific examples are as follows:
② slices can also specify a step size, for example, if the step size is 2, an element will be extracted from every other element between the start and end points. As shown below
The sequence is added. For example, the printout is "hello" + "word", and the printout is "hello word", but "hello" + [1jing2jing3] will report an error at run time, because this method is not supported.
Sequence multiplication: its main purpose is to extend, and this multiplication can also be applied to the value of None.
Sequence membership: if you want to see whether a member element is in the sequence, you can use the keyword in to determine. For example, I want to query whether element 5 is in the array [1Jing 4jue 5JEI 6], I can write this:
The printout of print (5 in [1re4je 5jpg 6]) is: True
Similarities and differences between lists and tuples
What they have in common: they are two of the python built-in sequences, and some operations on sequences in python can be used for both.
The difference: lists can be modified, while tuples are not. Therefore, it also determines that the application scenarios between the two are different, that is, the list is suitable for adding elements midway, and tuples are suitable for situations where modification of the sequence is prohibited for some reason.
Why do tuples exist?
1. According to its characteristics, it can be used as a key in the mapping, but the list cannot.
2. Some built-in functions and methods return tuples, which means we have to understand tuples.
List (modifiable)
Creation of the list: ① creates it manually directly, for example: num= [1, 2, 3, 4, 5]. ② uses strings to create lists and uses the list function. For example, list ("word"), the printout is as follows: ['wrestling, writing, etc.]
List basic operations:
① modifies the list (assigning values to list elements) as follows:
② deletes elements, mainly using the del method, as follows:
③ assigns values to slices. Using this function, you can insert and delete slices from the sequence. Examples are as follows:
> num= [1, 2, 3, 4]
> num1= [7, 8, 8, 9]
> num [3:3] = num1
> num
[1, 2, 3, 7, 8, 9, 4]
List-specific methods:
In addition to being able to use standard sequence operations, the list also has some unique operation methods of its own.
The ① append method, which is used to attach an object to the end of the list. As follows:
> list= [1pm 2pm 3]
> > list.append (5)
> list
[1, 2, 3, 5]
The ② clear method to clear the contents of the list.
> lst = [1,2,3]
> > lst.clear ()
> lst
[]
The ③ copy method, which is a regular assignment, knowledge associates another name to the list.
> a = [1,2,3]
> b = a
> b [1] = 4
> > a
[1, 4, 3]
The ④ count method, which mainly calculates the number of times a specified element appears in the list.
> x = [[1,2], 1,1, [2,1, [1,2]
> > x.count (1)
two
The ⑤ extend method is similar to append, but it can append multiple values to the end of the list at the same time.
> a = [1,2,3]
> b = [4,5,6]
> > a.extend (b)
> > a
[1, 2, 3, 4, 5, 6]
The ⑥ index method to find the index that the specified element appears in the list for the first time.
> knights = ['We',' are', 'the',' knights', 'who',' say', 'ni']
> knights.index ('who')
four
The ⑦ insert method to insert an object into the list.
> numbers = [1,2,3,5,6,7]
> numbers.insert (3, 'four')
> numbers
[1, 2, 3, 'four', 5,6,7]
The ⑧ pop method, which removes the last element from the list and returns this value.
> x = [1,2,3]
> > x.pop ()
three
The ⑨ remove method, which deletes the first element with the specified value
> x = ['to',' be', 'or',' not', 'to',' be']
> x.remove ('be')
> > x
['to',' or', 'not',' to', 'be']
The ⑩ reverse method sorts the elements in the list in reverse order, such as
> x = [1,2,3]
> > x.reverse ()
> > x
[3, 2, 1]
Tuple
1. Tuples and lists are similar, and elements are created and accessed in a similar way to lists.
2. Tuple creation: as long as some values are separated by commas, a tuple can be automatically created and enclosed in parentheses, for example:
1, 2, 3
(1, 2, 3)
3. Empty tuple, which means that it does not contain anything. Denoted by ().
4. The comma "," in the tuple is crucial. Just a comma can completely change the value of an expression. For example
> 3 * (40 + 2)
one hundred and twenty six
> 3 * (40 + 2,)
(42, 42, 42)
5. Function tuple: take a sequence as a parameter, convert it to a tuple, and return it intact if it is already a tuple.
> tuple ([1,2,3])
(1, 2, 3)
> tuple ('abc')
('averse,' baked,'c')
This is the end of this article on "what is the use of lists and tuples in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.