In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Python full stack cast how to achieve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python full stack cast how to achieve" it!
1. Number forced type conversion
Cast of Number type (int float complex bool)
# int forces data to be an integer "int float bool pure numeric string" var1 = 13var2 = 5.67var3 = Truevar4 = "123456" var5 = "123abc" var6 = 3+5jres = int (var2) res = int (var3) # True = > 1res = int (False) # False = > 0res = int (var4) # res = int (var5) error# res = int (var6) errorprint (res) Type (res)) # float forces data to decimal "int float bool pure numeric string" res = float (var1) res = float (var3) # True = > 1.0res = float (False) # False = > 0.0res = float (var4) # 123456.0 print (res Type (res)) # complex forces the data to be plural "" int float bool pure numeric string complex "res = complex (var1) # add 0j expression plural res = complex (var2) res = complex (var3) # True = > 1+0jres = complex (False) # False = > 0jres = complex (var4) # 123456+0jprint (res) Type (res)) # bool forces the data to be Boolean (in ten cases where Boolean is false) "Boolean can force all data types" 0,0.0, False, 0j'[] () set () {} None "res = bool (None) print (res, type (res)) # initialize the variable, it is not clear what value to use, and the brain writes None"None" for empty Stands for nothing, generally used to initialize the variable "" a = Noneb = None "", which is converted by default to a value of the current data type int () float () complex () bool () "" res = bool () print (res,type (res)) # extra extension "" strvar = "123" strvar = "3.134" strvar = "50003j" # res = int (strvar) # print (res,type (res)) # res = float (strvar) # print (res) Type (res) # res = complex (strvar) # print (res,type (res)) "2. Automatic type conversion _ container type conversion 2.1 Number automatic type conversion
Number automatic type conversion (int float complex bool)
Conversion from low precision to high precision bool-> int-> float-> complex "" # bool + intres = True + 100print (res, type (res)) # 1 + 100 = > 10 precision bool + floatres = True + 344.565 # 1.0 + 344.565 = > 345.565print (res, type (res)) # bool + complexres = True + 7-90j # 1 + 0J + 7-90j = > 8-90jprint (res Type (res)) # int + floatres = 5 + 7.88 # 5.07.88 = > 12.88print (res, type (res)) # int + complexres = 5 + 6 + 8j # 5 + 0j 6 + 8j = > 11 + 8jprint (res, type (res)) # float + complexres = 5.66 + 9.1-90j # 5.66 + 0j + 9.1-90j = > 14.76-90jprint (res Type (res): precision loss of decimal places (sometimes 15 / 18 decimal places are truncated after the decimal place, but not completely, precision loss exists) "" do not compare with decimal places, bite correctly "" print (0.10.2 = = 0.3) print (5.999 = 11.0) 0.09999999999999999999992container type conversion "
Cast of container types (str list tuple set dict)
Var1 = "I love you, Brother Wen" var2 = [1pje 2je 3] var3 = (4jue 4je 5) var4 = {"Chen Lu", "invigorating", "Liu Zitao", "reasonable"} var5 = {"cl": "gentle, gentle scum", "szq": "high achiever", "lzt": "basketball boy", "hl": "Wuda master"} var6 = True# str forced to convert to string "" all data types can be converted Enclose the current data type in quotation marks "" res = str (var2) res = str (var3) res = str (var4) res = str (var5) res = str (var6) res = str (var7) print (res, type (res)) # repr does not transfer character prototyping output string print (repr (res)) # list is forced to convert to a list "" if it is a string: take out each element in the string separately As a new element in the list, if it is a dictionary: keep only the keys in the dictionary if it is other container data: simply put [] parentheses "res = list (var1) res = list (var3) res = list (var4) # dictionary on both sides of the original data type: only get the keys in the dictionary, ignore the drop value res = list (var5) # res = list (var6) error can only be the print between containers (res) Type (res)) # tuple is cast into a tuple "" if it is a string: take out each element in the string separately as a new element in the tuple if it is a dictionary: keep only the keys in the dictionary if it is other container data: simply replace () parentheses "res = tuple (var1) res = tuple (var2) res = tuple (var4) res = tuple (var5) print (res) on both sides of the original data type Type (res)) # set is cast into a collection "" if it is a string: take out each element in the string separately as a new element in the collection if it is a dictionary: keep only the keys in the dictionary if it is other container data: simply replace {} parentheses "res = set (var1) res = set (var2) res = set (var5) print (res) on both sides of the original data type Type (res)) # filter out all duplicate elements in the list Lst =] res = set (lst) print (res) # converting the current collection to the original list res2 = list (res) print (res2) "" default does not add any value, converted to the data type null str () list () tuple () set () dict () "res = dict () print (res) print (type (res) 3. Strong turn of dictionary type _ type judgment 3.1 second-level container
Secondary container (list tuple set dict)
# second-level list lst = [1pje 2je 3, [4jre 5je 6]] # second-level tuple tup = (1jue 2, (10je 11)) # second-level set setvar = {1jue 2, ("a", "b")} # second-level dictionary dic = {"a": 1, "b": {"c": 10}} print (dic ["b") ["c"]) # level 4 container container = [1Jing 2J 3, (4J 5J 6, {"a": 1, "b": [11) "bingo"]})] # (4Jing 5print 6, {"a": 1, "b": [11, "bingo"]}) res1 = container [- 1] print (res1) # {'averse: 1,' bingo': [11, 'bingo']} res2 = res1 [- 1] print (res2) # [11 'bingo'] res3 = res2 ["b"] print (res3) # bingores4 = res3 [- 1] print (res4) # one-step abbreviation res = container [- 1] [- 1] ["b"] [- 1] print (res) # second-level container "the outside is a container, the elements inside are also containers, and the number of elements is the same"lst = [(1pr 2p3), [4Jing 5Jing 6], {7qu 8Yue 9}] 3.2 forced conversion of dictionary type
Cast of dict dictionary type
Requirements: it must be a second-level container of equal length, and the number of elements inside is 2; the outer layer is a list, tuple, collection, and the inner layer is a list or tuple equal second-level container = > dictionary; The outer layer is the list, and the inner layer is the list or tuple lst = [["a", 1], ("b", 2)] dic = dict (lst) print (dic, type (dic)) # {'a list: 1, 'baked: 2} # 2. The outer layer is a tuple, and the inner layer is a list or tuple tup = (["a", 1], ("b", 2)) dic = dict (lst) print (dic, type (dic)) # 3. The outer layer is the collection, and the inner layer is the tuple setvar = {("a", 1), ("b", 2)} dic = dict (setvar) print (dic, type (dic)) # exception 1: the outer layer is a list / tuple, and the inner layer is a collection "". It is not recommended to use "lst = [[" a ", 1], {" b "because the desired purpose cannot be achieved and the collection is disordered. Dic = dict (lst) print (dic) # exception 2: the outer layer is a list / tuple, and the inner layer contains the string "string length can only be 2 digits, which has great limitations. It is not recommended to use" lst = ["A1", "b2"] # lst = ["A11", "B22"] error # dic = dict (lst) # print (dic) Tip: the outer layer is a collection. It cannot be a list lst = [["a", 1], {"b", "250"}] dic = dict (lst) print (dic). It is possible to output {'avell (lst) print 250'} or ("a11") lst = [A1 "," b2 "] dic = dict (lst) print (dic) "b22"] dic = dict (lst) print (dic) the error will be reported here. Isinstance uses 3.3 more type judgments than type.
Judge type isinstance
"" # use method-isinstance (data, type) if the data is of this type, return True and vice versa, return False type: int float complex bool str list tuple set dict# uses method two isinstance (data, (type 1, type 2, type 3...). If the data is in the corresponding type tuple, return True, otherwise, return False "" # method-n = 123res = isinstance (n Int) print (res) n = [1je 2jue 3] res = isinstance (n, list) res = isinstance (n, tuple) print (res) # method 2 n = "1233" res = isinstance (n, (list, tuple, set, str) print (res) n = {"a": 1} res = isinstance (n, (list, tuple, set, str)) print (res) 4. A little exercise
The topics are as follows:
1. Which data can be forcibly transferred by intt _ focus _ float _ complex _ bool respectively. What are the values whose strong Boolean type is false? 3. How to obtain the condition of 4.bingo for forcibly changing the dictionary [1 bingo' 2: 3 bingo' 4, [5 recorder 6, 7, 8 recorder 9 10, (11, {"a": {"bingo'": 98}, "pp": {"d": 'bingo'}})]] 5. Which can't be converted into dictionaries and why? (1) {('adept) 1), (2) (2) {(' a)) (2) () (3) {('a)), "c3"} (4) {('a)) "b88"} Tip: in a file-5-positive infinity a = 3b = 3a and b id is the same a = b = 3 whether it is-5 to positive infinity An and b id are the same thank you for your reading, the above is the "Python full stack cast how to achieve" of the content, after the study of this article, I believe you on the Python full stack cast how to achieve this problem has a deeper understanding, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.