Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python rookie practice program


May 29, 2021 Article blog


Table of contents


For Python learners, mastering the simple and efficient programming skills in Python improves program efficiency and draws on the programmer's superior programming skills.

This article introduces ten small cases of Python, each with two workarounds. The first is relative to Xiaobai's writing, and the second is the experienced master's writing.

One, half a paragraph of a list of values are all less than a certain number

 Python rookie practice program1

Method 1: The most intuitive program is one to determine whether the elements in the list are less than a certain number, such a method is the easiest to think of, but the program is very complicated.

Method 2: Two Python built-in functions, Python anonymous functions, can be easily solved with one line of code.

Second, the string of the list is sorted according to specific requirements

 Python rookie practice program2

Method 1: Is to use the bubbling sort to solve;

Method 2: Just use the built-in function sorted one line of code to solve the problem. Not only that, but you can also set up your own sorting functions, such as the firstC function above, to sort by the first letter of the string for the specified aspects of the sorted keywords.

Third, sort the dictionary by key or numeric value

 Python rookie practice program3

Method 1: You can sort with the sorted built-in function and convert it to a dictionary. Such a way would result in a waste of space resources during the conversion process.

Method 2: It is directly using the key or value to sort, and then use the sorted key or value to construct the final dictionary, and the program is simple.

Fourth, replace the number of the list with a string

 Python rookie practice program4

Method 1: Take advantage of the list resolution scenario to generate new lists by iteratively.

Method 2: Use the map built-in function to convert the numbers in the list to strings.

Whether the five- and half-segment list elements are all of a type

 Python rookie practice program5

Method 1: The method used is to judge each element in the list on a case-by-case basis, and if either element is not a string, false is output. W hen all cycles are over, the output is True if the index value is equal to the total length of the list. d 2: The map function is still used to determine whether each element in the list meets the function checkStr. Use the all function to get the final result.

Six, reverse the list

 Python rookie practice program6

Method 1: The method used is to create a new list object and add the elements in list6 to the new list in a backward-forward manner.

Method 2: There are two ways, the first is to use the list slice method to get the reversed list. The second way is to take advantage of the list's reverse function, but the reverse function can only be modified in the original list, and a new list cannot be created.

Seven, from the iterative object randomly selected an element

 Python rookie practice program7

By selecting random values in Python, we can take advantage of Python's built-in library random, in the figure function above, the choice function randomly selects a number from the list, the choices function puts back the select k value, and sample is the select k value without playback.

Eight, use the list to create a dictionary

 Python rookie practice program8

Method 1: Create a dictionary with a list, and create a item of key-value pairs in the dictionary by looping for.

Method 2: Create a zip object with the zip built-in function, and use the dict function to convert the zip object into a dictionary, one line of code.

Nine, filter out the string that begins with the vowel letter

 Python rookie practice program9

Method 1: For each string element in list9, if the first letter of the string is the cause letter, add the string to the new list new_list9.

Method 2: The filter function can directly filter out the value of the element that meets the criteria compared to the map function, either directly using list resolution or filtering elements in list9 that meet the criteria.

Ten, create a counting dictionary

 Python rookie practice program10

Method 1: First create a dictionary whose key contains all the elements that have appeared in list10, and then count the number of times the elements in the list appear. Method 2: Borrows the Counter class in the collections library, counts the number of elements in list10 directly, and then uses the dict function to convert the Counter object into a dictionary object.

That's all you need to know about the Python rookie practice program.