Recently was using django developed the Web project time, discovered python some elementary knowledge grasping was not thorough, because at that time read time also did not have the multi-practices, therefore reorganized the python basic knowledge which some we could use frequently.
must say today is python in sets at function filter.
Python to have in one to construct the
filter function, it accepts two parameters: A function and a tabulation,
it returns to a tabulation.
took the first parameter transmits for
filter function itself should accept a parameter, this parameter will be in the second parameter tabulation item, the
filter returns tabulation can contain is spread the tabulation parameter to transmit all for
filter may by separate letter count returns really (true the element.
python code
- >>> def odd(n:
- … return n % 2
- …
- >>> li = [1, 2, 3, 5, 9, 10, 256, -3]
- >>> filter (odd, li
- [1, 3, 5, 9, -3]
filter accepts two parameters: A function (
odd and a tabulation (
li. It transfers the
odd function according to the tabulation circulation for each element. If the
odd returns is really (remembers, Python reveals all non-zero values for really, then this element is placed in the returns tabulation, is if not so is filtered. The result is one only contains in the original tabulation the odd number tabulation, appears then is in turn same with the original tabulation.