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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the knowledge points of python tuple". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Definition of tuple
A tuple is an immutable object that can hold 0 to more than one element (that is, a member of the tuple, also an object). The tuple itself cannot be modified. If you want to modify it, you can only create a new one. Its elements cannot be modified when they are immutable and can be modified when they are mutable.
Creation of tuples
Empty tuple, a pair of parentheses.
T = () print (type (t)) out:
A tuple of 1 element, enclosing the element in parentheses and followed by a comma
T = (1,) # Note that this comma is essential print (type (t)) T2 = (1) # No comma is the type of the data itself, not a tuple print (type (T2)) out:
A tuple of multiple elements that is surrounded by parentheses and separated by commas.
T = (1 print 2) 3) out (type (t))
Other types are converted to tuples
List1= set1= {1meme 2meme 3} dict1= {1min2 pint 3} t1=tuple (list1) t2=tuple (set1) t3=tuple (dict1) print (T1 (T1)) print (T2) print (t3) out: (1meme 2) (1) (1) 2) (1)
Addition and operation of tuples
Tuples are immutable objects and cannot add elements. If you need to add elements, you can only build a new one.
Tuples have + and * operations.
T1 = (1,2,3) print (T1, id (T1)) T1 + = (4,) print (T1, id (T1)) T1 = T1 * 2print (T1, id (T1) out: (1,2,3) 2276949975616 (1,2,3,4) 2276949965376 (1,2,3,4) 2276949924432 tuple deletion
Tuples are immutable objects whose elements cannot be deleted (whether the element is mutable or immutable).
The tuple itself can be deleted.
T = (1del 2, [3je 4]) # del t [0] will report an error, the element is an immutable object # del t [2] will report an error, and the element is a modification of the mutable object del t tuple
Tuples are immutable objects and cannot be modified. You can change it to a list and then change it to a tuple, which is actually a new tuple, not a modification.
T = (0,1,2) print (t, id (t)) T2 = list (t) # copy tuple t into a list to the new variable t2t2 [0] = 5 # this T2 is a list, its elements can be modified t = tuple (T2) # and then convert T2 into a tuple and assign a value to t, which is different from the id of the previous t. Print (t, id (t)) out: (0,1,2) 1766323347840 (5,1,2) 1766323347392
Elements are immutable objects that cannot be modified.
Element is a mutable object that can be modified
T1 = (1,2,3, [4,5]) # T1 [1] = 8888When it is an immutable object, it will report an error T1 [3] [0] = 6666.When it is a mutable object, it can modify the query of print (T1) out: (1,2,3, [666,5]) tuple
The query for tuples is an in operation.
T = (0,1,2) print (0 int) print (666int) print (888not int) built-in method of out:TrueFalseTrue tuple
Len, which returns the element length of the tuple
Index, which queries the location of an element in a tuple
Count, counting the number of occurrences of an element in a tuple
Here are some examples.
T = ('Today', 'Toutiao', 'about', 'tuple', 'tuple', 1, 1) print (t.index ('tuple')) print (t.count (1)) print (len (t)) out:327 tuple element acquisition method
Unpack, use _ to receive unwanted elements, add * to indicate that the variable is a list, can receive multiple elements, and can only receive one element without addition.
T = ('Today', 'Toutiao', 'about', 'tuple', 'tuple', 1, 1) # get the first two elements of t a, b, * = tprint (_, type (_)) print (a, b) # get the last two elements of t * _, num1, num2 = tprint (num1, num2) # get the penultimate element of t * _, ts, _ _ = tprint (ts) out: [about', 'tuples', 'tuples', 1, 1] Jinri Toutiao 11 tuples
Slice, slice remember 2 points, one is that the index starts from 0, the other is to open left and close right:
T = ('Today', 'headline', 'about', 'tuple', 'tuple', 1, 1) # print the first two elements of t print (t [: 2]) # print tprint (t [::-1]) # print the fourth and fifth elements of the tuple. Print (t [3:5]) out: ('Today', 'Toutiao') (1,1, 'tuple', 'tuple', 'about', 'Toutiao', 'Today') ('tuple', 'tuple') the nested structure of tuple t = (1, 2, 3), (4, 5, 6), (7, 8, 9), [10, 11] 12]) # print 6print (t [1] [2]) # change 10 to 666 Then print out t [3] [0] = 666print (t [3] [0]) out:6666 "what are the knowledge points of python tuple" and that's it. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.