ArticleDirectory
- Python zip Function Description
Python common functions: Python zip Function Description
The zip function uses any number of sequences as parameters. Combining all sequences into one element based on the same index is a new tuple sequence merged by each sequence, the length of the new sequence is subject to the shortest sequence of the parameter. In addition, the (*) operator can work with the zip function to implement the opposite function. The merged sequence is split into multiple tuple.
① New sequence of tuple
>>>> X = [1, 2, 3], y = ['A', 'B', 'C']
>>> Zip (x, y)
[(1, 'A'), (2, 'B'), (3, 'C')]
② The length of the new sequence shall be subject to the shortest sequence of the parameter.
>>>> X = [1, 2], y = ['A', 'B', 'C']
>>> Zip (x, y)
[(1, 'A'), (2, 'B')]
③ The (*) operator can work with the zip function to implement the opposite function. The merged sequence is split into multiple tuple.
>>>> X = [1, 2, 3], y = ['A', 'B', 'C']
>>>> Zip (* zip (x, y ))
[(1, 2, 3), ('A', 'B', 'C')]
2.Python built-in Function Map description
Map(Function,Iterable,...)
Map receives a function and an iteratable object (such as a list) as a parameter, processes each element with the function, and then returns a new list.
Assume that you read multiple rows of data from a file, and each row of data is in the following format:
59.4 60.4 58.4 120.2 121.2 119.2
Data = []
F=open('a.txt ')
For line in F. readlines ():
Data. append (MAP (float, line. Split ()))
The string split method is used to split the string into sequences.
3.Python provides a case-insensitive Conversion Method for string objects: Upper () and lower (). More than that, Python also provides us with the capitalize () method in upper-case letters, the capitalize () method in lower-case letters, and the title () method in lower-case letters.
S = 'Hello python' print S. Upper () print S. Lower () print S. capitalize () print S. Title ()
Output result:
Hello Python
Hello Python
Hello Python
Hello Python
Case sensitivity
Python provides the isupper (), islower (), and istitle () methods to determine the case sensitivity of strings. Note:If isupper (), islower (), istitle () is used as an empty string, the returned result is false.
4.* ARGs and ** kwargs usage in Python
Let's look at an example:
DefFoo (* ARGs, ** kwargs ):Print'Args = ', argSPrint'Wargs = ', kwargsPrint'---------------------------------------'If_ Name _ ='_ Main __': Foo (,) Foo (a = 1, B = 2, c = 3) Foo (, a = 1, B = 2, c = 3) foo ('A', 1, none, A = 1, B = '2', c = 3)
The output result is as follows:
ARGs = (1, 2, 3, 4)
Kwargs = {}
---------------------------------------
ARGs = ()
Kwargs = {'A': 1, 'C': 3, 'B': 2}
---------------------------------------
ARGs = (1, 2, 3, 4)
Kwargs = {'A': 1, 'C': 3, 'B': 2}
---------------------------------------
ARGs = ('A', 1, none)
Kwargs = {'A': 1, 'C': 3, 'B': '2 '}
---------------------------------------
As you can see, these two are variable parameters in Python. * ARGs indicates any number of unnamed parameters. It is a tuple. ** kwargs indicates a keyword parameter, which is a dict. When both * ARGs and ** kwargs are used, the * ARGs parameter column must be before ** kwargs, such as Foo (a = 1, B = '2', c = 3, if you call this method, a syntax error "syntaxerror: Non-Keyword Arg after keyword Arg" is displayed ".
You know what * ARGs and ** kwargs are. Another nice usage is to create a dictionary:
DefKw_dict (** kwargs ):ReturnKwargsPrintKw_dict (a = 1, B = 2, c = 3) = {'A': 1, 'B': 2, 'C': 3}
In fact, Python has a dict class. You can use dict (a = 1, B = 2, c = 3) to create a dictionary.