In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what python container types are, and the editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Python basic syntax-container
A container data type is a data type that can contain multiple data types.
1. List (list) 1.1 list basic concepts
The list is the container data type that comes with Python. The container is marked with "[]". Many elements in it are separated by commas: [element 1, element 2, element 3, …]
Features:
1. Variable order of elements
two。 Element content is variable
3. Variable number of elements
4. The list is ordered (subscript operations are supported).
Variability verification:
List1 = [] print (list1, type (list1))
Certificate result:
Ordered verification:
A = 100list2 = [1, 'ccc', a, axi2, a = 0, [a, 20]] print (list2)
Verification results:
Requirements for elements in the list:
1. Any type of data can be used as an element of a list. It can be an operation, not an assignment statement.
two。 Elements in the same list can have different data types.
3. Empty list: list = [], Boolean value is False
1.2 get elements
Subscript: also known as index, it is the unknown information of the element in the list
There are two kinds of subscript for ordered sequence in Python:
1. The subscript value that increases sequentially from left to right, starting at 0.
two。 The subscript value that decreases from right to left, starting at-1.
Subscript out of bounds: subscript exceeds the range of data in the list
Get a single element: only one element at a time.
Syntax:; list [subscript] gets the element corresponding to the specified subscript in the list.
Description:
1. List-can be a variable that holds a list, or it can be a list data
2. []-fixed writing method
3. Subscript-location information of the element in the list
Get multiple elements (list slices): take multiple elements at a time.
Syntax: list [start subscript: end subscript: step size] take from the start subscript to the end subscript, each time the subscript increases the specified step size.
Description:
1. The range and the direction of the range are determined by the start subscript and the ending subscript
two。 The acquisition interval and direction are determined by the step size.
3. A positive step means from the back, and a negative step means from back to front.
4. Whether the value can be obtained is determined by both the range direction and the step direction. If the range direction and step direction are the same, you can get the elements in the list; otherwise, it cannot be null.
5. When the value is not null, the element represented by the starting subscript is obtained, and the element represented by the ending subscript cannot be obtained.
Omit the action:
1: omit step size: list [start subscript: end subscript]
2: omit the starting subscript: list [: ending subscript: step size]
3: omit ending subscript: list [start subscript:: step size]
Traverse the list: take out the elements in the list one by one.
Syntax 1:
For variable in list:
Cyclic body
Verify:
Scores1 = [100,90,83,92,56,78,99,66,67,95,78] a = 0for x in scores1: if x > = 90: a + = 1print (a)
Verification results:
Syntax 2:
For variable in range (len (list))
List [variable]
1.3 add, delete, change 1.3.1 add-add elements
Syntax 1: list .append (element)
* * Verification: *
A = [1,2,3,4] a.append (5) print (a)
Verification results:
Syntax 2: list .insert (subscript, element)
Verify:
A = [1,2,3,4] a.insert (2,5) print (a)
Verification results:
1.3.2 Delete-Delete element
Syntax 1:del list [subscript]
Verify:
A = [1,2,3,4] del a [2] print (a)
Verification results:
Syntax 2: list. Remove (element)
Verify:
A = [1,2,3,4] a.remove (2) print (a)
Verification results:
Note:
An error will be reported if the element you want to delete does not exist; if there is more than one element in the list, delete only the first element.
Syntax 3:
Syntax 1: list .pop ()
Verify:
A = [1,2,3,4] a.pop () print (a)
Verification results:
Syntax 2: list .pop (subscript)
Verify:
A = [1,2,3,4] a.pop (2) print (a)
Verification results:
Note:
Pop does not delete elements directly. You can get the elements taken out by pop through variables.
Verify:
B = [1,2,3,4] a = b.pop () print (b) print (a)
Verification results:
1.3.3 change-change the elements in the list
Syntax: list [subscript] = value
Verify:
B = [1,2,3,4] b [0] = 2
Verification results:
This is the end of this article on "what are the types of python containers?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please 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.