Python code how to find the number of all daffodils
Aug 12, 2020 pm 02:57 PMPython 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 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] >>>
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))
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] >>>
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the advantages and disadvantages of templating?

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

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

A complete guide to golang function debugging and analysis
