Home>Article>Backend Development> How to output odd numbers in data in python

How to output odd numbers in data in python

尚
Original
2019-06-27 14:34:12 13321browse

How to output odd numbers in data in python

The filter() function is used to filter the sequence, filter out elements that do not meet the conditions, and return a new list composed of elements that meet the conditions.

This receives two parameters, the first is a function, and the second is a sequence. Each element of the sequence is passed to the function as a parameter for evaluation, and then returns True or False. Finally, the element that returns True is placed. to a new list.

The syntax of filter() function:

filter(function, iterable)

Parameters: function -- judgment function. iterable – Iterable object.

Return value: Return list.

The following shows an example of using the filter function:

Filter out all odd numbers in the list:

#!/usr/bin/python # -*- coding: UTF-8 -*- def is_odd(n): return n % 2 == 1 newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) print(newlist)

Output results:

[1, 3, 5, 7, 9]

More For many Python related technical articles, please visit thePython Tutorialcolumn to learn!

The above is the detailed content of How to output odd numbers in data in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn