In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the usefulness of Python lists. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The Python list is a location-dependent game collection of any type of object. It has no fixed size and can be assigned by offsets.
List operation
In / not in determines whether the element exists in the list
If (1 in a): print ("in")
List generator
[x for x in range (10)]
A = list (range (10)) # Loop range (10), and save each element to generate a new list, equivalent to list (range (10)) list1 = [i for i in range (10)] # Loop range (10) And save the square of each element to generate a new list list2 = [iTuni for i in range (10)] print (range (10)) print (a) print (list1) print (list2) # output range (0,10) [0,1,2,3,4,5,6,7,8,9] [0,1,2,3,4,5,6,7,8,9] [0,1,4,9,16,25,36, 49, 64, 81]
Len: find the length
Index:
> a = [1jin2 index 3] > > a [1pje 2je 3] > > a [0] > 1 > > a [0] = 0 # the value of the index number corresponding to the list can be changed through a [index] > a [3] = 4 # if the index number does not exist, an error Traceback (most recent call last): File ", line 1, in IndexError: index >
Slice: return to a new list without changing the original list
[start:end:step] start position: end position: step size
If the step size is negative, it will be cut backwards.
> > a [0,2,3] > > a [:] # [:] take the entire index [0,2,3] > > a [1:3] # slice from index 1 to index 2, and return a new list [2,3] > > a [1:] [2,3] > > a [0:] [0,2,3] > > a [0:-1] [0,2] specific operations of the list:
Add elements: change the original list
Append (): add other elements to the end of the list index. The added elements occupy only one index position, changing the original list.
A = [1pyrm 2pr 3] b = [4je 5je 6] a.append (b) [1pr 2m 3, [4je 5je 6]]
Extend (): appends a sequence (iterble) to the end of the list, appending each element in the sequence
# extend () > > a = [1min2 name' 3] > a.extend ([3pence5] > > a] > > a.extend ({'name':'lee','age':18}) > > a [1d2 name' 3,3 beer5 name', 6,' age', 'name'] > > a.extend ((7)) > > a [1mr2je 3, 3pyrr5] >' age', 'name', 7 pencils 8]
Insert (index, element) add an element anywhere
A = [1m 2m 3] a.insert (0m m 1) a.insert (5m 4) # out of range, add # output to the end by default [- 1m 1m 2m 3e 4]
+: the two lists are added together. If it is not for the list, an error will be reported. The original list will not be changed and a new list will be generated.
+ =: equivalent to extend ()
Inser (index,object): add a value to the index location
Add elements to slices
A = [1Jing 2jue 3] b = [4je 5je 6] a [1:] = b # is equivalent to replacing all the elements in the list a [1] with print (a) # output [1,4pje 5Jet 6]
Difference:
Both append and insert, + = will change the original list, while + will only generate a new list without changing the size of the original list.
+ must be followed by a list, and others can be any element
> a = [1 abc' 2 str 3] > a.append ('str) # add elements from the end > a [1pi 2jue 3,' abc'] > a > line 1, in TypeError: can only concatenate list (not "str") to list > a + ['456'] [1Jing 2jue 3,' abc', '456']
Delete elements: change the original list
Del (a): delete the entire list
Del (a [0]): delete the first element in a list, same as pop (0)
Pop (n): delete elements in list pinning (index)
Remove (value): delete values from the list
Clear (): deletes all values in the list
List other methods:
Sort (): sorts the list in ascending order. Only the same type can be sorted, such as the list is full of numbers or letters.
Reverse (): flip the list, which can contain different elements
> b = [1 b.sort () # to sort B in ascending order > b () # to flip B > b > c > > c.reverse () # to flip C > c]
The difference between the two: sort () requires the elements in the list to be of the same type, while reverse () does not need them, but simply flips the position of the elements in the list.
Sort () supports passing a key as the specified parameter to sort.
A = [{'id':11}, {' id':33}, {'id':2}] a.sort (key=lambda a.sort [' id']) print (a)
Index (value): finds the subscript position of the element in the list. If it exists, it returns the index position. If it does not exist, an error is reported. If there is the same value, the subscript of the first value is returned by default.
Index (value,start,end): start end looks from the fixed (starting subscript, ending subscript) subscript location, and there is no error report.
A = ["hello", "world", 3, "hello"] b = a.index ("hello") print (b) # output 0, there are two hello. By default, the first subscript c = [1, 2, 4, 5, 6] c.index (2, 2, 4) # reports an error. Look for a subscript with a value of 2 from subscript 2 to 4, because there is no error. # c [2] is 3 c [4] is 5
Count (value): individuals who have an element in a statistical list
Copy (): copy list
Thank you for reading! This is the end of this article on "what is the use of Python list?". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can 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.