Home Backend Development Python Tutorial Python code how to find the number of all daffodils

Python code how to find the number of all daffodils

Aug 12, 2020 pm 02:57 PM
python

Python code to find all daffodil numbers: first use the list to push through all three-digit numbers, and mark each number; then filter out the tuples marked as True; and finally the first sentence Put the second value of the filtered tuple into the list structure and add a print statement.

Python code how to find the number of all daffodils

Python code to find the number of all daffodils:

Method one:

>>> 
>>> a = list(map(lambda x: x[1], filter(lambda x: x[0], [(i*100+j*10+k == i**3+j**3+k**3, i**3+j**3+k**3) for i in range(1, 10) for j in range(0, 10) for k in range(0, 10)])))
>>> print(a)
[153, 370, 371, 407]
>>>
Copy after login

Related learning recommendations: python video tutorial

Instructions:

The above code can be broken down into three lines of code:

a = [(i*100+j*10+k == i**3+j**3+k**3, i**3+j**3+k**3) for i in range(1, 10) for j in range(0, 10) for k in range(0, 10)]
b = filter(lambda x: x[0], a)
c = list(map(lambda x: x[1], b))
Copy after login

The first sentence means using a list to push through all three digits, and each number is marked. If the daffodils are counting, mark True, if not, mark False. The marks and numbers are put into one ancestor: ( flag, value), all tuples are placed in a list structure.

The second sentence means to filter out the tuples marked as True.

The third sentence means c.

Add the print statement and execute the above three lines of code to understand.

Method 2:

Only use list derivation.

>>> a = [i**3+j**3+k**3 for i in range(1, 10) for j in range(0, 10) for k in range(0, 10) if i*100+j*10+k == i**3+j**3+k**3]
>>> print(a)
[153, 370, 371, 407]
>>>
Copy after login

Related recommendations: Programming video course

The above is the detailed content of Python code how to find the number of all daffodils. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

A complete guide to golang function debugging and analysis

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

See all articles