You’re going to get some python practice these days, so find an exercise site and plan on doing 3-5 problems a week.

www.runoob.com/python/pyth…

In addition, the site also has Python tutorials, ranging from basic to advanced.


Example-1 Three-digit combination

Title: There are four numbers: 1, 2, 3, and 4. How many different and non-repeating three-digit numbers can be formed? What are they?

Train of thought

The simplest method is the exhaustive method, respectively in the hundreds, tens, ones digit, and then eliminate the combination of repeated digits, the rest is the answer.

Code implementation

The direct code implementation is as follows:

def create_three_digits(number_start=1, number_end=4):
    ' ''Given the specified number range (e.g., 1 to 4), find the number of non-repeating three-digit numbers that can be formed :param number_start: start number :param number_end: end number :return number, and a list of possible three-digit numbers'' '
    count = 0
    result_list = list()
    for i in range(number_start, number_end + 1):
        for j in range(number_start, number_end + 1):
            for k in range(number_start, number_end + 1):
                if(i ! = j) and (i ! = k) and (j ! = k): count += 1 result_list.append(str(i) + str(j) + str(k))return count, result_list
Copy the code

To make it easier, we can use the list derivation:

def create_three_digits2(number_start=1, number_end=4):
    ' ''List deduction :param number_start: :param number_end: :return:'' '
    return [str(i) + str(j) + str(k) for i in range(number_start, number_end + 1) for j in
            range(number_start, number_end + 1) for k in
            range(number_start, number_end + 1) if(i ! = j) and (i ! = k) and (j ! = k)]Copy the code

The output is as follows, with 24 different permutations and combinations.

valid count=24, and they are:
123
124
132
134
142
143
213
214
231
234
241
243
312
314
321
324
341
342
412
413
421
423
431
432
Copy the code

Of course, the time complexity of the current code implementation is very high, after all, three for loops. If you have a better solution, let me know in the comments!

Knowledge review – list derivation

List comprehensions (also known as list parsing) provide a succinct way to create lists.

It is structured by enclosing an expression in brackets, followed by a for statement, followed by zero or more for or if statements. That expression can be arbitrary, which means you can put any type of object in the list. The result will be a new list, generated after the expression in the context of the if and for statements has been run.

To express the list in code, the derivation is as follows:

variable = [out_exp for out_exp in input_list if out_exp == 2]
Copy the code

A concise example is as follows:

multiples = [i for i in range(30) if i % 3 is 0]
print(multiples)
# Output: [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
Copy the code

So, when is the best time to use a list derivation?

This is when you need to use the for loop to generate a new list. For example, you usually do this:

squared = []
for x in range(10):
    squared.append(x**2)
Copy the code

In this case, the list derivation is most appropriate:

squared = [x**2 for x in range(10)]
Copy the code

Source code at:

Github.com/ccc013/Code…


Reference article:

  • List comprehensions

Welcome to follow my wechat official account – Machine Learning and Computer Vision, or scan the qr code below, we can communicate, learn and progress together!

Past wonderful recommendation

Learning notes
  • Introduction to Machine Learning series 1 – An Overview of Machine learning
  • Getting to know GAN
  • GAN Learning Series 2: The Origin of GAN
  • [GAN Learning series 3] Image Restoration using Deep Learning and TensorFlow
Math study notes
  • Math Notes for programmers 1- Base conversion
  • Programmer’s Math Note 2– remainder
  • Mathematical Notes for Programmers 3– Iterative methods
Github projects & Resource tutorials recommended
  • [Github Project recommends] a better site for reading and finding papers
  • TensorFlow is now available in Chinese
  • Must-read AI and Deep learning blog
  • An easy-to-understand TensorFlow tutorial
  • Recommend some Python books and tutorials, both beginner and advanced!