In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces "how to use the python list" in detail, the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use the python list" can help you solve your doubts.
Create a list list () # create a list list (iterable objects) # create iterable objects into a list
Slice index: list [aVOB]
Slice index assignment: list [slice] = iterable object
L = [2pyrm 3rem 4] L [0:1] = [23je 34] print (L) # replace the first one in L with [23je 34] print (L) # [23je 34,3pyr4] L [2:] = [3.3mil 4.4pr 5.5] # [23je 34,4pr 5.5] print (L) L [:] = [3met 4] L [0:0] = [1,2] # [1,2mil] 4] add [1Magne2] L [: 0] = "ABC" # before index 0 iteratively add "ABC" print (L) # ['A', 'Bond,' C é cor, 1] L = [3,5] L [1:1] = [4.1,4.2] # [3,4.1,4.2] 5] add L [1:2] = [] # after index 1 to delete index 1L = [1, 2, 3, 4, 5, 6] L [::-2] = "DEF" # [1, 'Founding, 3,' eject, 5,'D']
Note: the step size is not equal to the slice assignment, but the iterative object should be equal to the number of slices cut out.
Common function
These function lingua franca other iterable objects (str, list, tuple, bytes, bytearray)
Len (x): returns the length of the sequence
Max (x): returns the maximum element of the sequence
Min (x): returns the minimum element of the sequence
Sum (x): returns the sum of all elements in the sequence (elements must be of numeric type)
Any (x): truth test, returning True if one of the values in the list is true
All (x): truth test, returning True if all values in the list are true
Common methods
L.index (v, begin, end): returns the index subscript of the corresponding element v. Begin is the start index and end is the end index. ValueError error is triggered when value does not exist.
L.insert (index, obj): inserts the obj element into the specified location in the list
L.count (x): returns the number of elements x in the list
L.remove (x): deletes the value that appears in the list for the first time. X is the index that specifies that the value is not the list, unlike pop, it does not return any value.
L2=L.copy (): copy this list (only one layer, not deep objects). If the root changes, the shallow copied list will change, the shallow copied list is connected to the root, and the deep copied list creates another same root.
L.append (x): appends a single element to the list, not two elements, but tuples of two elements
L.extend (lst): appends another list to the list
L.clear (): clear the list, which is equivalent to L [:] = [] empty list
L.sort (reverse=False): sorts the elements in the list, the default order is in the order of values, and sorts L, but does not return any values
Sorted (L): returns the sorted object
L.reverse (): the inversion of the list, which is used to change the order of the original list. This function does not return a list, but an iterator. You can use list to convert the returned objects into a list.
L.pop (index): delete the element corresponding to the index. If the index is not indexed, the last element will be deleted by default, and the deleted element returned at the same time
List derivation
List derivation is an expression that generates a list from iterable objects.
Syntax: [expression for variable in iterate object] or [expression for variable in iterate object if truth expression]
Note: the if clause of for in expression can be omitted, after which all generated objects will be evaluated.
# generate a list of values 1 to 9 squared L = [xylene 2 for x in range (1,10)] print (L) # [1, 4, 9, 16, 25, 36, 49, 64, 81] L = [x for x in range (1, 10) if x% 2 = = 1] print (L) # [1,3,5,7 9] list-inferred nested # [expression for variable 1 in iterable object 1 if truth expression 1for variable 2 in iterable object 2 if truth expression 2] L = [x + y for x in "ABC" for y in "123"] # ['A1,'A2,'A3,'B1,'B2, B3, C1, C2 The text parsing method of 'C3'] string str.split (sep = None)
Use sep as the delimiter to split the str string, and use the blank character as the separator when no parameter is given. Return the list of segmented strings and the list of string changes. You can only split the spaces in the string. Spaces can only be found in split (").
Str2 = "wo ai ni" L = str2.split (") # ['wo',' ai', 'ni'] str1 =" wo,ai,ni "print (str1.split (',')) # ['wo',' ai', 'ni'] print (" www "," baidu "," com ") # www baidu comprint (" www "," baidu "," com ", sep=". ") # set the delimiter www.baidu.com
Str.join (* *)
Insert the str string into the delimiter of * * and replace the delimiter in * * with str
S ='\\ '.join ([' Programe Files', 'python3']) print (s) # C:\ Programe Files\ python3 Deep copy shallow copy
Shallow copy means that the copied object points to the root object. When the root object changes, both the original and shallow copied objects will change.
L = [3.1,3.2] L1 = [1,2, L] L2 = L1.copy () # shallow copy L2 [2] [0] = 3.14print (L1) # [1,2, [3.14,3.2]] print (L2) # [1,2,3.14,3.2] Deep copy
Deep copy is to recreate a root. When the original root changes, the deeply copied root will not change, so the value of the deep copy will change, and the original value will not change.
Import copy # Import copy module, and then import L = [3.1,3.2] L1 = [1,2, L] L2 = copy.deepcopy (L1) # Deep copy L2 [2] [0] = 3.14print (L1) # [1,2, [3.1,3.2]] #
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.