In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the practical skills of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the practical skills of Python"?
1. List derivation
Some people may have heard of this before. One of the encoding modes in Python is very common and needs to be modified. Creating lists using for loops is the value of list derivation.
The list is readable and concise, and it does take some time to understand what's going on. The following is an in-depth study of whether this problem can be solved.
List derivation should be considered when creating a list. List derivation starts with a set of square brackets. Forget the grammar in order to show the similarities between the two languages
List_comp = [expression for itemin items]
The task is to replace a for loop that adds items to the list.
New_list = [] for item in old_list: new_list.append (item**2)
Note:
Call the name new_list is calledt
For loops through items in the old list
Use the add method to add item * * 2 to the new list
So far, it's simple.
The list derivation is as follows
New_list = [item**2 for item inold_list]
Note:
Use the expression item * * 2 as the expression to add to the list, which first appears in the list derivation.
The following for loop is the loop to be iterated.
Enclosing it in square brackets and assigning the variable new_list means that the expression item * * 2 is added to the new_list for each item of the loop.
There is no additional method or no need, a line of code is just right! In order to select what to add, you can even add conditional statements. This feature is also extended to collections and dictionaries! You can even write list deductions using nested for loops!
Please note that overuse may occur! List derivation is used only if the list loop is simple and it is clear that you want to create a list.
two。 Generator expression-Saving memory usage
When you have a large number of datasets and need to iterate over or get results from them, but you cannot store all the data in memory, you should think of generator functions. Except for switching for the keyword yield without returning, the generator function is very much like a function.
The generator function creates an iterator, and the iterator needs to force the output of the values it has. They do not store objects in lists or collections. Only one item is output at a time. This is called a delay loop. Therefore, the generator is used when reading large files.
Def gen (n): while True: yield n n + = 1G = gen (3) # starts at 3 print (next (G)) # 3 print (next (G)) # 4 print (next (G)) # 5 print (next (G)) # 6
Note:
Create function gen
The Yield keyword stores the value n until the next () method is called
Add 1 to n using the assignment operator
It's an infinite cycle, and it lasts all the time.
Call the next () method, which will continue to output values
For now, these values are not stored and only pop out when the next () method is called. The generator creates an iterator and then relies on the next () method to output the value.
This is useful when you have a big data set and want to stream data without overflowing memory.
3. Iterate over two objects using zip
You often want to iterate over multiple objects so that you can collect data from each object. Zip is the function that allows you to do this! You can do this if you need to iterate to fully understand the file name and its corresponding links.
For file_name, link in zip (names, links): print (file_name, link)
This enters two objects at the same time, returning a tuple and the corresponding item in each tuple. In the loop, the tuple is extracted into separate values file_name and link. Zip (). You can select as many collections as you want, but it stops when the shortest collection is exhausted.
Through the list derivation mentioned in this article, the Zip function can be used to iterate over pairs of elements in the same object.
For example:
Differences = [next_el-elt for el, next_el in zip (items, items [1:])
4. Counter-the count of generated objects
A counter is a subclass of a dictionary where the element of the object is the key and the count of items in the object is the value. It is valid for counting the number of objects. The built-in collections module needs to be imported when accessing the counter.
Suppose you want to calculate all the strings that appear in the list.
Import collections counts = Counter ([Fred, Samantha, Jean-Claude, Samantha]) print (counts)
Output:
Counter ({Samantha: 2, Fred: 1, Jean-Claude: 1})
The advantage of the counter class is that it can be updated and values can be accessed using the dictionary API.
From collections import Counter c = Counter (abcdaab) for letter in abcde: print (letter,:, c [letter])
Output:
A: 3 b: 2 c: 1 d: 1 e: 0
Note:
Use the string "abcdaab" to assign c to the countermeasure subclass. The counter class provides a separate count of each character. C [item] can be accessed like a dictionary.
The for loop, which iterates over the string "abcde" and assigns letter to each string.
Enter string characters, print a variable letter and act like a dictionary. The count of each letter is accessed by c [letter].
5. Link multiple collections
If you need to iterate through more than one collection at a time, the linking method in the itertools module is a good way to achieve this goal.
For name in itertools.chain (first_name_list, second_name_list): create_person (name)
Before running out, this iterates through the first collection, then moves on to the next, and so on.
Thank you for your reading, these are the contents of "what are the practical skills of Python?" after the study of this article, I believe you have a deeper understanding of what practical skills of Python have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.