In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use Python lists and tuples". In daily operation, I believe many people have doubts about how to use Python lists and tuples. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to use Python lists and tuples". Next, please follow the editor to study!
1. Python list (list) 1. Sequence introduction
Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its location, or index, with the first index being 0 and the second index being
1, and so on. Python has built-in types for six sequences, but the most common are lists and tuples. Operations that can be performed in a sequence include indexing, slicing, adding, multiplying, and checking members. In addition, Python has built-in methods to determine the length of the sequence and to determine the largest and smallest elements.
Python: variable sequence and immutable sequence
Variable sequence: lists, collections, dictionaries can be added, deleted and modified, and the sequence address page will not change.
Immutable sequence: no addition, deletion or modification operations for tuples and strings
# variable sequence: list, dictionary, collection list_1 = [10Let20 list_1 30] print (id (list_1)) # list_1 address: 2927159489792list_1.append (40) print (id (list_1)) # list_1 address: 2927159489792 changed the value The list address has not changed # immutable sequence: string, tuple str_1 = "HelloWorld" print (id (str_1)) # str_1 address is: 2244899454896str_1 = str_1+ "1" print (id (str_1)) # str_1 address is: 2244900762928 changed value, string address also changed 2. An overview of a list A list is an ordered sequence of 0 or more elements, belonging to the sequence type.
Features:
a. The length and content of the list are variable (you can freely add, delete or replace the elements in the list)
b. There is no length limit for the list
c. The element types in the list can be different (basic data types, lists, tuples, dictionaries, collections, and other custom types of objects)
3. Create a list
To create a list, simply enclose the different data items separated by commas in square brackets. As follows:
# create a list method-list _ 0 = [] # empty list list_1 = ['elephan','monkey','tiger'] list_2 = [1Magi 2Magazine 3] list_3 = [1Magi 2.2 minicertigerjournal journal Truelist 1] # list can store different data types # create list method 2: use the list () built-in function list (obj) function to convert strings, range () objects, tuples and other objects into lists. List_4 = list ("python") list_5 = list (list_6 = list ((1Med 2)) list_7 = list (range (1) 5)) 4. Index of the list # Index of the list list_1 = [1Magne2.2] print (list_1 [0]) print (list_1 [3]) # print (list_1 [4]) reported an error, exceeding the length of the list by 5. Slicing of the list # the shard of the list list_1 = print (list_1 [1:3]) print (list_1 [0:3:2]) print (list_1 [- 1 list_1]) # when reverse sharding, note that the step size should be negative 6. Fragmented assignment of the list x = [1meme2magor3] x [1] = 1 # replace the index print (x) x [2:] = 5pai6 # replace print (x) x [1:1] from the index numbered 2 below = [1magee 2mei 3mei 5mei 6mei 7] # insert "1pr 2pr 4mem 5m 5m 7" print (x) # running results [1,1,3Jol 4] [1,1,5] 6] [1, 1, 2, 3, 4, 5, 6, 7, 1, 5, 6] 7. Loop through the list
In order to effectively output the data in the list one by one, you can use while and for loops to facilitate the output of the list.
The while loop traverses the list:
X = [1, length, 2, 3, 4, 5, 6, 7, 8 and 9] length = len (x) I = 0while I
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.