In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the query operation and slicing method of python list", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what is the query operation and slicing method of python list"?
1. List
Lists are python built-in data structures (lists, tuples, dictionaries, collections), which are equivalent to arrays
All the data in the list is arranged in order, and the list belongs to the sequence type.
All data in the list has positive and negative indexes, which can always be mapped to uniquely determined data through the specified index.
Duplicate data can exist in the list (distinguish between set)
Any type of data can be saved in the list, and multiple types of data can be mixed and stored in one list.
The list can be dynamically scaled and scaled as needed, and the system will dynamically allocate and reclaim memory as needed. There is no need to pre-declare the capacity of the list before use.
two。 Creation of list [] or list ()
Create a list with square brackets []. Do not name the variable list or lline list is the corresponding class name of the list. L can easily be mistaken for the number 1.
Call the built-in function list () (constructor of class list)
L = [] # create an empty list L = [1 list (L) #] print (L) # [1 list () #) # introduce the range object [1 list () #) # to create an empty list [1] list () # create an empty list
3. Locate the element L [0] in the list
Use the index to get the elements of the list, and if the specified index does not exist in the list, the error IndexError: list index out of range is thrown
4. Query the element index L.index () in the list
The first element index is 0, and the subsequent elements are + 1 in turn.
The last element index is-1, and the first element is-1.
L = ['Hutchison L.index (' L1') L.index ('h') L.index ('h') # value errorL.index ('LBJ 2) # start with index 2 to find' l'L.index ('lest') # look for'ln'in [2,4]
Call method index to get the index of the specified element in the list, and only return values greater than 0, such as L.index ('e') = 1. If there are multiple specified elements in the list, the method index only returns the index value of the first specified element, such as L.index ('l') = 2. If the specified element does not exist in the list, the error ValueError:'h' is not in list is thrown.
When calling the method index, you can also specify two parameters, the start index start and the end index stop, to find elements within a certain range (excluding the value at index stop)
5. List slicing operation L [start: stop:step]
Syntax format of slices [start:stop:step]
The resulting slice is still a list, a copy of the fragment of the original list
The resulting slice does not include the elements corresponding to the index stop
The default value of step step is 1, which can be simplified to [start:stop]
When step is positive
If you do not specify start, the first element of the slice defaults to the list being the first element
If you do not specify stop, the last element of the slice defaults to the last element of the list
Slices are calculated later from index start
When step is negative
If you do not specify start, the first element of the slice defaults to the last element of the list
If you do not specify stop, the last element of the slice defaults to the first element of the list
Calculate slices forward from index start
L = list ('HelloWorld') L [1:7:2] L [1:6] L [:] # return the entire list enter L [] error SyntaxError: invalid syntaxL [:-1] # flip the entire list L [:-1] # stop is specified as the element of-1 [' hashes, 'estranges,' lags, 'lags,' oaths, 'walls,' oaks,'r' 'l'] L [6: 0 2] L [0: 6 2] # start is specified as the element where 0 resides Looking forward with no value, return [] L [8::-2] # ['lumped,' oiled, 'oiled,' lumped,'H'] L [8: 0 Musi 2] # ['lumped,' oiled, 'oiled,' l'] does not contain the element L [- 2] specified by stop [- 2] L [: 3]
Slicing operation allows indexes to be out of bounds (contrast index is not allowed)
L = list ('HelloWorld') L [: 100] L [- 100:]
6.L [slice (start,stop,step)]
You can call the built-in function slice (the constructor of class slice) to create objects of type slice
There are three ways to call the built-in function slice
Slice (stop)
Slice (start,stop)
Slice (start,stop,step)
The default values for start, stop, and step are all None
Slice (start,stop,step) is equivalent to start:stop:step in slicing operation
L = list ('HelloWorld') L [slice (1Magazine 9)] L [1: 9:2] L [:] L [slice (None,None,None)] # L [slice (None)] returns the entire list L [1:7] L [slice (7)] L [: 7] L [slice (7)] # you can enter only stop or write L [slice (None, 7)]
7.in/not in query whether it contains an element. It returns TrueL = list ('HelloWorld') print (5 in L) # False.
At this point, I believe that everyone on the "python list query operation and slicing method is what" have a deeper understanding, might as well to the actual operation of it! 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.