This is my sixth day of the Gwen Challenge

1 table derivation

We often encounter problems with data processing, such as extracting all the data in a list that meets a condition:

Eg: Extract all numbers greater than 1 from [1, -1, 0, 6, -2, -3, 4, 8] by iterating through the for loop:

What’s wrong with this code?

1.Running speed is slow2.The code appears redundant3.Added new list variablesCopy the code

If you want to rewrite the above code, you can do so using python’s own list expression

Or use the filter function in Python:

Why use the above method?

1.Python's built-in functions generally run at close to C speed, so speed increases2.The code is concise and easy to read3.No additional variables need to be addedCopy the code
Tips: List parsing is faster than Filter, but you can be sure that both methods are faster than iteration.Copy the code

List parsing can also be used like this:

You can see in the figure above that there are two pieces of code that do exactly the same thing: get all the text content under an xpath node fetched from LXML. However, table table expressions are not only faster, but also concise, using different code for the same requirements, while the second one is arguably good code.

In addition to list parsing, you can also use dictionary parsing to retrieve dictionary data that meets certain criteria: it is written as follows:

Iteritems () {k: v for k,v in dict.iteritems() if v > 90} where k is the key and v is the valueCopy the code

Finally, there is collection parsing, which is written like list parsing

{x for x in set if x % 3 == 0}
Copy the code

The simplification of expressions, that’s why I say good syntax is important.

Use default iterators and operators

This typically happens with types that support default iterators and operators, such as list, dict, or files.

Operators: in and not in

Why we use it: The default operators and iterators have no extra method calls and are more efficient. It always feels strange to emphasize efficiency in Python. And functions that use the default operator are generic and can support any type of operation.

Cons: You may not be able to distinguish object types by method names

Usage scenario: If the type support should take precedence, it’s just that you can’t modify the container while iterating through it.

eg:   for key in a_dict:
          ...
          if key not in a_dict: 
              ...
          if obj in a_list: 
              ...
      for line in a_file: 
          ...
      for k, v in dict.iteritems(): 
          ...
Copy the code

Examples of bad

No:   for key in a_dict.keys(): 
          ...
      if not a_dict.has_key(key): 
          ...
      for line in a_file.readlines(): 
          ...
Copy the code

3 Use None as little as possible

The result of a when the code is not the ideal results, we (I) before at least None will use to as a return value return, behind actually think this is not the right thing to do, in the face of special circumstances, we should use exceptions to say it, because in the conditional expression, None and 0, an empty string, will be sentenced to false, this is the place where life, You may not know what your code actually produces!! So use None less.