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/02 Report--
In this issue, the editor will bring you an example analysis of the built-in functions of python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Range function
Be able to generate a specified sequence of numbers
Use case:
'' range (start,stop,step) parameter: start: start value, default value is 0 stop: end value step: optional, step value default value is 1 return value: iterable object, number sequence''# range function is used # write only one parameter, that is, from zero to before 10, 9 res = range (11) # when two parameters, the first parameter is the start value The second parameter is the value of the end (before the end value) res = range (5Power10) # three parameters, parameter 1 is the start value, parameter 2 is the end value, parameter 3 is the step value res = range (1Magazine 10Cool 3) # get a flashback number sequence res = range (10memo 0lame 1) res = range (10cor 0lim 2) res = range (- 10flint 20lim 1) res = range (- 20Lint 10) res = range (- 10L10) print (list (res))
The method of extracting the sequence of numbers returned by the range () function:
# the method of getting the sequence of numbers returned by the range function res = range (10)
List list data
Print (list (res))
Traversing through a for loop
For i in res: print (I)
Turn to an iterator and use the next function to call
Res = iter (res) print (next (res)) print (next (res)) zip () function
The zip function accepts multiple iterable objects, and then combines the I th element of each iterable object to form a new iterator.
Example:
Parameter: * iterables, any iterable object
Return value: an iterator that returns a tuple
Var1 = '1234'var2 = [' axiaojia'] var3 = (''axiomagy,''B',')
Call the zip function to form a new tuple iterator
Res = zip (var1,var2,var3) # print (res,type (res)) for i in res: print (I)''('1','a','A') ('2','b','B') ('3','C','C') ('4','d','D')''
Zip (), combined with the * operator, can be used to disassemble a list:
X = [1,2,3] y = [4,5,6] zipped = zip (x, y) print (list (zipped)) print (zip (x, y)) # iterator object, print (* zip (x, y)) # combined multiple tuple data other built-in functions
Built-in functions related to data type conversion
Int () converts other types of data to integers
Float () is converted to a floating point type
Bool () converted to Boolean type
Complex () becomes plural
Str () converted to string type
List changes to list type
Tuple is converted to tuple type
Dict changes to dictionary type
Set is converted to collection type
Variable correlation function
Id () gets the ID identity of the current data
Type () gets the type string of the current data
Printing of print () data
Input () gets the input data
Isinstance () detects whether it is the specified data type
Mathematical correlation function
#
Get the absolute value of a number print (abs (- 99.9999)) # Sum the items in iterable from left to right from start and return the total value print (sum ([1MJ 2) 3]) # get the maximum print (max ([1J 2J 3])) print (max (99J 12) 45) # get the minimum value print (min ([2Q 1J 6 Lok 9])) print (min (6 Mol 7 Jo 1 Jue 0) (- 2)) # the power operation returns the y power print of x (pow (2p3)) # rounded r = round (3.1415926) r = round (3.1415926) # the decimal point retains several digits r = round (4.5) # odd forward and even backward 1. 5 = 22. 5, 2, 3, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 2, 4, 4, 4, 4, 2, 2, 4, 4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 5, 4, 4, 4, 2, 2, 4, 4, 4, 2, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 5, 4, 4, 4, 2, 4, 4, 4, 2, 4, 4, 5, 4, 4,
# bin () convert numeric types to binary print (bin (123)) # 0b1111011# int () convert binary to integer print (int (0b1111011)) # 123 # oct () to octal numbers 01234567 print (oct (123)) # 0o173# hex () to hexadecimal numbers 0123456789abcdef print (hex (123)) # 0x7b# convert characters to asciir = ord ('a') print (r) # convert ascii to words Sign r = chr (65) print (r) higher order function
Sorted (iterable, [reverse,key])
Take out the elements in the iterable data one by one, put them into the key function for processing, and sort them according to the results of return in the function to return a new list.
Function: sorting
Parameters:
Iterable iterable data (container type data, range data sequence, iterator)
Reverse is optional. Whether to reverse or not. Default is False. No reversal, True reversal.
Key optional, function, can be a custom function or a built-in function
Return value: sorted result
Arr = [3, 7, 1, 1, 9, 20, 10]
Sort from small to big by default
Res = sorted (arr) # [- 9,1,3,7,10,20]
Can be sorted from large to small
Res = sorted (arr,reverse=True) # [20,10,7,3,1,-9]
Use the function abs (absolute value) as the key keyword parameter of sorted
Res = sorted (arr,key=abs) print (res)
Use custom functions
Def func (num): print (num,num% 2) return num% 2 arr = [3, 2, 4, 6, 5, 7, 9]
Use custom functions to process data in sorted functions
Res = sorted (arr,key=func) print (res) # optimized version arr = [3, arr,key=lambda x:x%2, 4, 5, 7, 9] res = sorted (arr,key=lambda x:x%2) print (res) map (func, * iterables)
Each element in the incoming iterable data is put into the function for processing and a new iterator is returned.
Parameters:
Func function Custom function | built-in function
Iterables: iterable data
Return value: iterator
(1) convert a list of string numbers into a list of integers
# [1] for i in varlist: newlist.append (int (I)) print (newlist) the common method of handling is varlist = [1, 2, 2, 4, 4, 5, 5, 4, 3, 4, 4, 3, 4, 5, 4, 5, 5, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 4, 4, 3, 4].
Use the map function for processing
Varlist = ['1] res = map (int,varlist) # print (list (res))
(2) [1, 2, 3, 4] = > [1, 4, 4, 9, 16]
Common method
Varlist = [1Jing 2 Jing 3 Jing 4] newlist = [] for i in varlist: res = I * * 2 newlist.append (res) print (newlist)
Use the map function to process this data
Varlist = [1 myfunc,varlist 2 3 varlist 4] def myfunc (x): return x * * 2 res = map (myfunc,varlist) print (res,list (res))
Optimized version
Res = map (lambda XRV x Varlist) print (res,list (res))
Practice homework
['axiaqingmei'] = > [65pd6 / 67pm68]
Reduce (func,iterable)
Each time we take two elements from iterable and put them into the func function to get a calculation result, and then put the calculation result and the third element in iterable into the func function to continue the operation, and the result and the fourth element after that are added to the func function for processing, and so on, until the final element is involved in the operation.
Parameters:
Func: built-in or custom functions
Iterable: iterable data
Return value: the final operation result
Note: when using the reduce function, you need to import from functools import reduce
From functools import reduce
(1) [5, 2, 1, 1] = > 5211
Common method
Varlist = [5 for i in varlist: res + = str (I) res = int (res) print (res,type (res)) '521 15 * 10 + 2 = = 5252 * 10 + 1 = 521521 * 10 + 1 = = 5211
Using reduce to complete
Def myfunc (XMague y): return x*10+yvarlist = [5 mine2 pyrrine 1]
Call function
Res = reduce (myfunc,varlist) print (res,type (res))
(2) change the string's' 456'= > 456
How to solve the above problem when it is required that the int method cannot be used for type conversion
Define a function that returns an integer number given the number of a string
Def myfunc (s): vardict = {# # * * $$} {0 # # * * $$} 10: 1 return vardict (s): 5: 5: 6: 7: 7: 8: 8: 9]
First use the map function to convert a numeric string into an integer number.
Iter1 = map (myfunc,'456')
Use lambda to reprocess the values in the number list
Iter2 = reduce (lambda x print (iter2) filter (func,iterable)
Filter the data, take each element in iterable to the func function for processing, retain the data if the function returns True, and discard the data if False is returned.
Parameters:
Func custom function
Itereble: iterable data
Return value: an iterator of retained data
It is required to keep all even numbers and discard all odd numbers.
Varlist = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Common method implementation
Newlist = [] for i in varlist: if I% 2 = 0: newlist.append (I) print (newlist)
Use filter for processing
Define a function to determine whether the current function is even. Even returns True and odd returns False.
Def myfunc (n): if n% 2 = 0: return True else: return False
Call the filter function to process
It = filter (myfunc,varlist) print (it,list (it))
Optimized version
It = filter (lambda n:True if n% 2 = = 0 else False,varlist) print (it,list (it)) the above is the example of python built-in function that Xiaobian shared with you. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.