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

What are the knowledge points of python list

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "what are the knowledge points of the python list?" in the operation of actual cases, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Definition of list

Lists are the most commonly used Python data type, and lists are mutable objects that can hold 0 to multiple elements (that is, members of the list, which are also objects). A list is an ordered sequence in which each element in the sequence is assigned an index, with the first index 0, the second index 1, and so on. The elements of the list can be inserted, added, deleted, or modified. Common operations are indexing, slicing, adding, multiplying, checking members, etc. Python has built-in methods to determine the length of the list and to determine the maximum and minimum values of list elements.

Creation of list

Empty list, a pair of parentheses.

T = [] print (type (t)) out:

A list of 1 elements, surrounded by square brackets.

T = [1] print (type (t)) out:

A list of multiple elements, surrounded by square brackets and separated by commas.

T = [1 type 2 3] print (type (t)) out:

Other types are converted to lists.

Tuple1 = (1,2,3) set1 = {1,2,3} dict1 = {1:'a', 2: 'baked, 3:' c'} list1 = list (tuple1) list2 = list (set1) list3 = list (dict1) print (list1, type (list1)) print (list2, type (list2)) print (list3, type (list3) out: [1, 2, 3] [1, 2, 3] [1, 2, 3]

List derivation.

List1 = [i for i in range (10)] list2 = [i for i in range (10) if I% 2 = = 0] list3 = [I + j * 2 for i in range (3) for j in range (3)] list4 = [(I, j, k) for i in range (1) for j in range (1) for k in range (1) if I = = j] print (list1, type (list1)) print (list2, type (list2)) print (list3, type (list3)) print (list4 Type (list4)) out: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 2, 4, 6, 8] [0, 2, 4, 1, 3, 5, 2, 4, 6] [(1, 1, 2), (2, 2, 2)]

The addition and operation of list

Lists are mutable objects that can be added at the end of the line, which is extremely efficient. You can also insert elements, but it is inefficient because a large number of movement operations occur during insertion, and it is not recommended to insert elements unless necessary.

List1 = [1,2,3] print (list1, id (list1)) list1.append (4) print (list1, id (list1)) list1.extend ([5mem6]) list1.insert (0.5) list1.insert (3d2.5) print (list1, id (list1) out: [1,2,3] 2984233256832 [1,2,3,4] 2984233256832 [0.5,1,2,2.5,3,4,5]] 2984233256832]

Lists have + and * operations, but this is not recommended because the * operation is actually a new list and is inefficient; although the + operation is not a new list, it runs much faster than the built-in append method of c code. It is strongly recommended to use append and extend to add elements.

List1 = [1,2,3] print (list1, id (list1)) list1 + = (4,) print (list1, id (list1)) list1 = list1 * 2print (list1, id (list1)) out: [1,2,3] 1367941064832 [1,2,3,4] 1367941064832 [1,2,3,4] 1367941065472

Deletion of list

Del: delete the element based on the index value, or delete the list itself directly.

List1 = [i for i in range (5)] del list1 [0] print (list1) del list1out: [1,2,3,4]

Pop (): deletes the element based on the index value, or deletes the end-of-line element if you do not specify an index. The pop method has a return value.

List1 = [i for i in range (5)] print (list1.pop (), list1) print (list1.pop (0), list1) out:4 [0,1,2,3] 0 [1,2,3]

Remove (): deletes based on the value of the element, using this method when the indexed index only determines the value. It is important to note that the remove method deletes only the first matching element. If the value does not exist in the list, a ValueError error is raised.

List1 = [i for i in range (3)] * 2print (list1) list1.remove (2) print (list1) # list1.remove (3) ValueError: list.remove (x): x not in listout: [0,1,2,0,1,2] [0,1,0,2]

Clear (): clear the contents of the list, keep the list itself, and use this method first when you need to empty the list in the body of the loop, rather than creating a new list, because it is relatively expensive.

List1 = [i for i in range (5)] list1.clear () print (list1) out: []

Modification of the list

The list is a mutable object, and the contents of the list can be modified by subscript and slicing methods to confirm the scope of modification.

List1 = [i for i in range (5)] list1 [0] = 'a'list1 [1:3] =' bc'print (list1) out: ['await,' baked, 'caged, 3,4]

Query of list

The query for the list is an in operation.

List1 = [i for i in range (5)] print (5 in list1) print (5 not in list1) print (1 in list1) out:FalseTrueTrue

Built-in methods for lists

There are a lot more built-in methods in lists than tuples, in addition to the above mentioned append, extend, instert, remove, pop, clear, there are also some sequence general methods len, index, count, copy, list specific methods sort, reverse.

Len, which returns the element length of the list

Index, which queries the location of an element in the list

Count, which counts the number of occurrences of an element in the list

Copy, create a copy of the list, the content is consistent, the id is different. (note: deepcopy should be used for nested structures)

Here are some examples.

List1 = ['Today', 'headlines', 'about', 'list', 'list', 1, 1] print (list1.index (list)) print (list1.count (1)) print (len (list1)) list2 = list1list3 = list1.copyprint (id (list1), id (list2), id (list3)) list2 = list1 # is directly equivalent to the same idlist3 = list1.copy # copy copied. But id is different from print (id (list1), id (list2), id (list3)) out:3272270698482048 22706984820482270698383456

Sort, sort list elements, syntax demonstration, list.sort (key=None, reverse=False), key can specify index method (lambda expression or custom method), reverse specifies whether to sort in reverse order. Sort directly changes the order of the original list.

Sorted, which is basically similar to sort, but does not change the order of the original list, but returns a new list and defines additional variables to receive the sort results.

Reverse, the list elements are arranged inverted by index, and reverse changes the order of the original list directly.

Using the slicing method list [::-1], you can also sort the list elements in reverse order by index, but do not change the original list order, and define additional variables to receive the sort results.

List1 = [3,5,6,1,2,4] list1.sort () print (list1) out: [1,2,3,4,5,6]

List1 = [3,5,6,1,2,4] list2 = sorted (list1) print (list1) print (list2) out: [3,5,6,1,2,4] [1,2,6]

List1 = [3,5,6,1,2,4] list1.reverse () print (list1) out: [4,2,1,6,5,3]

The element acquisition method of the list (same as tuple)

Unpack, use _ to receive unwanted elements, add * to indicate that the variable is a list, can receive multiple elements, and can only receive one element without addition.

T = ['Today', 'headlines', 'about', 'list', 'list', 1, 1] # get the first two elements of list t a, b, * = tprint (_, type (_)) print (a, b) # get the last two elements of list t * _, num1, num2 = tprint (num1, num2) # get the penultimate element of list t * _, ts, _ _ = tprint (ts) out: [about', 'list', 'list', 1, 1] Jinri Toutiao 1 1 list

Slice, slice remember 2 points, one is that the index starts from 0, the other is to open left and close right:

T = ['Today', 'headlines', 'about', 'list', 'list', 1, 1] # print the first two elements of the list t print (t [: 2]) # print list tprint (t [::-1]) # print the fourth and fifth elements of the list. Print (t [3:5]) out: ['Today', 'headlines'] [1,1, 'lists', 'lists', 'about', 'headlines', 'Today'] ['list', 'list']

The nested structure of the list (like tuples) t = [(1, 2, 3), (4, 5, 6), (7, 8, 9), [10, 11, 12]] # print 6print (t [1] [2]) # change 10 to 666. then print out t [3] [0] = 666print (t [3] [0]) out:6666 "what are the knowledge points of the python list". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report