Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the indexing and slicing of Python list

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge points about the indexing and slicing of the Python list. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

What is an index?

Which data types have the concept of index? -> string, list, tuple

The index starts from the position of the leftmost record.

The index is represented by a number, with the starting bit starting at 0

The maximum index of strings, lists, and tuples is their length-1

Examples are as follows:

Names = ['Neo',' Jack', 'Adem'] print (names [0]) # > > Neoprint (names [- 1]) # > > Ademprint (names [5]) # IndexError: list index out of range does not have an element with an index of 5, so what is a slice with an error?

Indexes are used to access individual members (elements), while slices are used to access members (elements) within a certain range.

Slices use colons to find members (elements) in two index positions apart in square brackets, such as [0:10]

The rules of slicing: left contains, right does not contain; left contains, right does not contain

The complete list obtained by slicing is no longer the original list, even if it is the complete contents of the original list.

Examples are as follows:

Num_list = [1,2,3,4,5,6,7,8,9,10] print (num_list [3:7])

The implementation results are as follows:

[4, 5, 6, 7]

The full content of the get list is as follows:

Names = ['Neo',' Jack', 'Adem'] print (' the complete content of the names list is:', names [:]) print ('the complete content of the names list is:', names [0:])

The implementation results are as follows:

> the complete content of the names list is: ['Neo',' Jack', 'Adem']

> the complete content of the names list is: ['Neo',' Jack', 'Adem']

The complete list obtained by slicing is no longer the original list, even if it is the complete contents of the original list.

Examples are as follows:

Names = ['Neo',' Jack', 'Adem',' Lily'] print (the memory address of the'\ 'names\' list is:', id (names)) print (the memory address of the complete\ 'names\' list obtained by the index is:', id (namespace [0:])) # the execution result is as follows: the memory address of the names' list is: 14052294968915 memory > the memory address of the complete 'names' list obtained by the index is: 140522949686656

Get list members (elements) in reverse order, as shown below:

Num_list = [1,2,3,4,5,6,7 8] reverse order print of print (num_list [:-1]) # reverse acquisition of list print (num_ list [0: 8:2]) # step size of list: [every 2 steps (which can also be understood as every two values) gets elements from index 0 to 8] # the execution result is as follows: # > > [8,7,6,5,4,3,2] 1] # > > [6, 7] # > > [1, 3, 5, 7]

The slice generates an empty list, as shown in the following example:

Num_list = [1, 2, 3, 4, 5, 6, 7, 8] print (num_list [0:0]) # the execution result is as follows: # > [] list index, get and modify

The list.index (item) list passes in an element to get the index value of the current element through the index () function.

List [index] = new_item; list [index] is the value of the index corresponding to the variable; new_item is a new element

The scope of data modification can only be within the existing index range.

The list cannot be assigned by adding a new index

Examples are as follows:

Test_str = ['averse,' baked, 'clocked,' d'] print (test_str.index ('c')) # the execution result is as follows: # > 2'c' the index position is 2test_str = ['a', 'baked,' clocked,'d'] print (test_str.index ('e')) # the execution result is as follows: # > > ValueError: 'e'is not in listtest_str = [' a` The execution result of 'baked,' clocked,'d'] test_str [0] = 'z'print (test_str) # is as follows: # > [' zoned, 'baked,' clocked,'d'] test_str = ['averse,' baked, 'clocked,' d'] test_str [:] = 'hacked,' jacked,'k' The execution result of 'l'print (test_str) # is as follows: # > [' hashes, 'jacks,' kills,'l'] test_str = ['ajar,' breadth, 'cations,' d'] test_str [:] = ['oaks,' packs, 'qcodes,' r'] print (test_str) # the execution results are as follows: # > > ['oaks,' packs,'q' 'r'] test_str = ['await,' baked, 'cached,' d'] test_str [5] = 'z'print (test_str) # the execution result is as follows: # > IndexError: list assignment index out of range deletes the index through the pop () function

The function of the pop () function: delete and get the elements of the list through the index

Usage of the pop () function: list.pop (index), index is the element of the delete list

Function deletes the elements of the index and returns

If the incoming index index does not exist, an error will be reported

Examples are as follows:

Names = ['Neo',' Jack', 'Adem',' Lily'] pop_item = names.pop (1) print ('deleted element is:', pop_item,', deleted\ 'names\' list is:', names) # the execution result is as follows: # > deleted element is: Jack, deleted 'names' list is: [' Neo', 'Adem',' Lily'] names = ['Neo',' Jack' 'Adem',' Lily'] pop_item = names.pop (5) print (names) # the execution result is as follows: # > IndexError: pop index out of range deletes the index through del

The function of the del function: delete and get the elements of the list through the index

Usage of the del function: del list (index), index is the element of the delete list

Delete directly, no return value

If the incoming index index does not exist, an error will be reported

Examples are as follows:

Names = ['Neo',' Jack', 'Adem',' Lily'] del names [0] print (names) # execution result is as follows: # > [Jack', 'Adem',' Lily'] names = ['Neo',' Jack', 'Adem',' Lily'] del names [0] print (names) # execution result is as follows: # > > IndexError: particularity of list assignment index out of range index in tuple

You can get indexes and slice indexes just like lists

The use of the tuple function index and list is exactly the same.

Elements cannot be modified or deleted through the index (because tuples are immutable)

These are all the contents of the article "how to index and slice the Python list". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report