In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of Python list operation, the article is very detailed, has a certain reference value, interested friends must read it!
Preface
Multiple objects with different data types can be stored in the list
The memory space of an object:
So the memory space for a list is:
A = 10lst = ['hello',' a', 'world'] print (lst) print (type (lst)) print (id (lst)) print (' lst [0] type:', type (lst [0]))
I. the creation of the list:
Memory schematic diagram:
# the first way to create a list: lst = ['hello','world','52525'] print (lst) # the second way to build the function list () lst1 = list [' nice','to','meet','you'] # is not list [] print (lst1) lst2 = list (['nice','to','meet','you']) print (lst2) print ("lst2 [0]:" Lst2 [0]) print ("lst2 [- 4]:", lst2 [- 4])
Second, the characteristics of the list:
3. Query operation
# query print (lst.index ('52525')) # find print in a certain range (lst.index (4p0Pol 5)) # index is value,start,stop, respectively
Get multiple elements in the list: slicing operation
Slice result! It is equivalent to returning a new list of elements of the original list.
Lst = ['hello','world','52525',123,4,2] print (lst) print (lst [0:3]) print (LST [0: 4:2])
Note: when the x:x:-x, that is, the step size is negative, it is equivalent to start from back to front
Print ("step size is negative") print (lst [::-1]) print (LST [3: 1 print]) 4. Traversal of the list: lst = ['hello','world','52525',123,4,2] print (lst) for item in lst: print (item)
5. Additions, deletions and modifications of list elements (1) increase
Memory process of append ()
Lst = ['hello','world','52525',123,4,2] print (lst,id (lst)) lst.append (100) print (lst,id (lst)) # you can see that the address has not changed lst1 = [' new list' 'to insert'] lst.append (lst1) # insert lst1 as an element in the list lst (append can insert only one element) print (lst) lst.extend (lst1) # insert multiple elements print (lst) lst.insert (0Magne90) # insert (index,value) print (lst) # slice lst3 = ['True','False','List'] lst [1:] = lst3print (lst) at the end of the lst
(2) deletion
Lst = ['hello','world','52525',123,4,2] print (' remove delete:') print (lst) lst.remove ('hello') # Delete a specified element print (lst) print (' pop delete:') # pop () delete lst.pop (1) # delete '5252'print (lst) print (' slice delete (generate new list):') # slice # A new list object is generated at this time Does not meet our expectations new_list = lst [1:4] print ("new_list:", new_list) print (lst) print ('slice deletion (does not produce a new list) Delete the original list and replace it with []):') # there is no need to generate a new list, explain the original list with multiple elements lst [1:4] = [] print (lst)
VI. List generation
List generation is required only if there are certain rules for the elements in the list.
# list generation lst = [i for i in range (0Magne11)] print (lst) lst = [iTuni for i in range (0Magne11)] print (lst)
The above is all the contents of the article "sample Analysis of Python list Operation". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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: 290
*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.