Python hide the advanced features you get to it?

Python and more easy to use Needless to say, look at the language they used to know. But Python to hide the advanced features you get it? In this paper, the author enumerates five kinds of Python slightly advanced features as well as methods for their use, come check it out now!

Python is a beautiful language, it is easy to use but very powerful. But you really use all the features of Python do?

Advanced features of any programming language are usually through a lot of experience only to find. For example, you are writing a complex project, and find answers to a question on stackoverflow. Then you suddenly found a very elegant solution, which uses the Python function you never know!

This method of learning is so interesting: by exploring, occasionally find anything.

Below are five kinds of advanced features Python, as well as their usage.

Lambda function

Lambda function is a relatively small anonymous functions - Anonymous means that it does not actually function name.

Python functions typically use def a_function_name () to define the style, but for lambda functions, we did not name it. This is because the function lambda function is to perform some simple expressions or operations, without completely defined functions.

lambda functions can be used any number of arguments, but only one expression.


1.  x = lambda a, b : a * b 
2.  print(x(5, 6)) # prints '30' 

4.  x = lambda a : a*3 + 3 
5.  print(x(3)) # prints '12' 

Look how simple it! We carried out some simple math, without the need to define the entire function. This is one of the many features of Python, these features make it a clean, simple programming language.

Map function

Map () function is a built Python, a function which can be applied to various elements in a data structure, such as lists or dictionaries. For this calculation, it is a very clean and readable implementation.


1.  def square_it_func(a): 
2.  return a * a 

4.  x = map(square_it_func, [1, 4, 7]) 
5.  print(x) # prints '[1, 16, 47]' 

7.  def multiplier_func(a, b): 
8.  return a * b 

10.  x = map(multiplier_func, [1, 4, 7], [2, 5, 8]) 
11.  print(x) # prints '[2, 20, 56]'看看上面的示例!我们可以将函数应用于单个或多个列表。实际上,你可以使用任何 Python 函数作为 map 函数的输入,只要它与你正在操作的序列元素是兼容的。 

Filter function

filter with built-in function is very similar to the map function, it will also function to the sequence structure (lists, tuples, dictionaries). The key difference is that the filter () will only return the application function returns True elements.

For more details, see the following example:


1.  # Our numbers 
2.  numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 

4.  # Function that filters out all numbers which are odd 
5.  def filter_odd_numbers(num): 

7.  if num % 2 == 0: 
8.  return True 
9.  else: 
10.  return False 

12.  filtered_numbers = filter(filter_odd_numbers, numbers) 

14.  print(filtered_numbers) 
15.  # filtered_numbers = [2, 4, 6, 8, 10, 12, 14] 

Not only do we evaluated the True or False for each list element, filter () function also ensures that only the return match for the True elements. Very easy to check the expression and processing returns to build a list of these two steps.

Itertools module

The tool module is the Python Itertools processing iteration is set. Iterator is a type of data may be in the loop for (including a list of tuples, and dictionaries) used.

Use function Itertools module allows you to perform many iterator operations that usually require multiple lines of a list of functions and complex to understand. About Itertools magic, consider the following example:


1.  from itertools import * 

3.  # Easy joining of two lists into a list of tuples 
4.  for i in izip([1, 2, 3], ['a', 'b', 'c']): 
5.  print i 
6.  # ('a', 1) 
7.  # ('b', 2) 
8.  # ('c', 3) 

10.  # The count() function returns an interator that  
11.  # produces consecutive integers, forever. This  
12.  # one is great for adding indices next to your list  
13.  # elements for readability and convenience 
14.  for i in izip(count(1), ['Bob', 'Emily', 'Joe']): 
15.  print i 
16.  # (1, 'Bob') 
17.  # (2, 'Emily') 
18.  # (3, 'Joe')     

20.  # The dropwhile() function returns an iterator that returns  
21.  # all the elements of the input which come after a certain  
22.  # condition becomes false for the first time.  
23.  def check_for_drop(x): 
24.  print 'Checking: ', x 
25.  return (x > 5) 

27.  for i in dropwhile(should_drop, [2, 4, 6, 8, 10, 12]): 
28.  print 'Result: ', i 

30.  # Checking: 2 
31.  # Checking: 4 
32.  # Result: 6 
33.  # Result: 8 
34.  # Result: 10 
35.  # Result: 12 

38.  # The groupby() function is great for retrieving bunches 
39.  # of iterator elements which are the same or have similar  
40.  # properties 

42.  a = sorted([1, 2, 1, 3, 2, 1, 2, 3, 4, 5]) 
43.  for key, value in groupby(a): 
44.  print(key, value), end=' ') 

46.  # (1, [1, 1, 1]) 
47.  # (2, [2, 2, 2])  
48.  # (3, [3, 3])  
49.  # (4, [4])  
50.  # (5, [5])  

Function Generator

Generator function is a function similar to iterator that it can also be used in the for loop statement. This greatly simplifies your code, and compared to a simple for loop, it saves a lot of memory.

For example, we want all the numbers 1-1000 are added, the first part of this block of code to show you how to use a for loop to perform this calculation.

If the list is small, such as 1000 rows, the required computer memory okay. But if the giant long list, such as one billion floating-point numbers, this problem will appear. Use this for loop, a large number in the memory list, but not everyone has unlimited RAM to store so many things. Python in the range () function is so dry, it builds a list in memory.

Code shows the use of a second portion of the digital Python generator function that sums. generator function to create an element, and only if necessary in memory that stores one at a time. This means that if you want to create one billion floating-point numbers, you can put them one at a time in memory! Python 2.x in xrange () function is to use a generator to build a list.

These examples show: If you want to generate a list of a wide range, then you need to use the generator function. If you have limited memory, such as the use of mobile computing devices or edge, using this method is particularly important.

That is, if you want to list a number of iterations, and it is small enough to put in the memory, it is best to use a for loop or Python 2.x function of range. Because the generator function and xrange function will be generated every time you visit their new list of values, and Python 2.x range function is static list, and integer been placed in memory for quick access.


1.  # (1) Using a for loopv 
2.  numbers = list() 

4.  for i in range(1000): 
5.  numbers.append(i+1) 

7.  total = sum(numbers) 

9.  # (2) Using a generator 
10.  def generate_numbers(n): 
11.  num, numbers = 1, [] 
12.  while num < n: 
13.  numbers.append(num) 
14.  num += 1 
15.  return numbers 
16.  total = sum(generate_numbers(1000)) 

18.  # (3) range() vs xrange() 
19.  total = sum(range(1000 + 1)) 
20.  total = sum(xrange(1000 + 1)) 

We will certainly encounter many difficulties when learning python, as well as the pursuit of new technologies, here's what we recommend learning Python buckle qun: 784758214, here is the python learner gathering place! ! At the same time, he was a senior development engineer python, python script from basic to web development, reptiles, django, data mining and other projects to combat zero-based data are finishing. Given to every little python partner! Daily share some methods of learning and the need to pay attention to small details

Click: Python exchange technology sharing

Guess you like

Origin blog.csdn.net/zhizhun88/article/details/90707062