In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what is the use of the tuple list in Python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Tuples in Python are similar to lists, except that the elements of tuples cannot be modified.
Tuples use parentheses and lists use square brackets.
Tuple
Tuple is represented by parentheses or no parentheses and is a series of sequential numbers.
A_tuple = (12, 3, 5, 15, 6)
Another_tuple = 12, 3, 5, 15, 6.
Print (a_tuple)
Print (another_tuple)
"
(12, 3, 5, 15, 6)
(12, 3, 5, 15, 6)
"
List
List is named after square brackets
Adept list = [12pr 3pr 5pr 15pl 6]
Print (a_list)
"
[12, 3, 5, 15, 6]
"
List add
Add at the end
Adept list = [12pr 3pr 5pr 15pl 6]
A_list.append () # append a 0 after a_list
Print (a_list)
"
[12, 3, 5, 15, 6, 0]
"
Specify location to add
Adept list = [12pr 3pr 5pr 15pl 6]
A_list.insert (1,) # add 0 at location 1
Print (a_list)
"
[12, 0, 3, 5, 15, 6, 0]
"
List removal
Delete item
Adept list = [12pr 3pr 5pr 15pl 6]
A_list.remove (3) # Delete the first item in the list with a value of 3
Print (a_list)
"
[12, 5, 15, 6]
"
List index
Show specific location
Adept list = [12, 3, 5, 15, 6, 4, 7]
Print (a_list [0]) # displays the value of bit 0 of the list a_list
# 12
Print (a_list [- 1]) # displays the value of the last bit of the list a_list
# 7
Print (a_list [0:3]) # displays the values of all items in the list a_list from bit 0 to bit 2 (before bit 3)
# [12, 3, 5]
Print (a _ list [5:]) # displays the values of bit 5 and all subsequent items in the list a_list
# [4, 7]
Print (a_list [- 3:]) # displays the values of the penultimate digit of the list a_list and all subsequent items
# [6, 4, 7]
Print the index of a value in the list
Adept list = [12, 3, 5, 15, 6, 4, 7]
Print (a_list.index (3)) # displays the index of the item with a value of 3 that first appears in the list a_list
# 1
Count the number of times a value appears in the list
Adept list = [12, 3, 5, 15, 6, 4, 7, 6, 6]
Print (a_list.count (6)) # count the number of occurrences of 6 in the list a_list
# 4
List sorting
_ list= [12,3,5,15,6,4,7,6,6,6]
A_list.sort () # sort from smallest to largest by default
Print (a_list)
# [3, 4, 5, 6, 6, 6, 6, 7, 12, 15]
A_list.sort (reverse=True) # sort from largest to smallest
Print (a_list)
# [15, 12, 7, 6, 6, 6, 6, 5, 4, 3]
Comparison between List and Tuple
Their elements can be iterated, output, applied, and positioned one by one.
List iteration
For list_item in a_list:
Print (list_item)
"
twelve
three
five
fifteen
six
"
Tuple iteration
For the next example, output the elements in a_tuple and a_list in turn:
For index in range (len (a_list)):
Print ("index =", index, ", number in list =", a _ list [index])
"
Index =, number in list = 12
Index = 1, number in list = 3
Index = 2, number in list = 5
Index = 3, number in list = 15
Index = 4, number in list = 6
"
For index in range (len (a_tuple)):
Print ("index =", index, ", number in tuple =", a _ tuple [index])
"
Index =, number in tuple = 12
Index = 1, number in tuple = 3
Index = 2, number in tuple = 5
Index = 3, number in tuple = 15
Index = 4, number in tuple = 6
"
At this point you should know the difference between tuples and lists.
This is the end of this article on "what is the use of tuple lists in Python?". I hope the above content can be helpful 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.