In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to master all the contents of Python built-in zip ()". The content in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how to master all the contents of Python built-in zip ()".
1. N uses of zip ()
Basic usage: like a zipper, multiple iterable objects can be combined and then taken out in turn with a for loop, or the results can be stored in a container such as a list, tuple, or dictionary at once.
The result is an iterator, the element generated by the iterator is a tuple, and the elements of the first tuple come from the first element of the iterable object parameter, as shown in the figure above.
In addition, the for loop can also pull out the elements in the tuple in turn, which is convenient:
Its parameters are not required to be iterable objects of the same class, so there are many ways to combine them, such as:
But what happens if you take the dictionary as an argument to zip ()? Dictionaries are in the form of key-value key-value pairs, which are different from the structure of single elements such as lists.
As you can see from the experiment, zip () only traverses the dictionary's key value by default:
If you want to fetch the value value of the dictionary, or the key-value key-value pair, you can use the dictionary's own traversal methods values () and items ():
Using zip (), you can also easily convert rows and rows to a two-dimensional list:
The asterisk (*) operator in the above example can unpack (unpacking) the elements of my_list (also a list) into multiple parameters to zip (), thus reassembling the three lists.
The unpacking operator is also applicable to zip objects, because zip () itself is a row-column conversion operation. If you unpack it and give it to zip () as a parameter, you are doing another row-column transformation, that is, going back to the origin (except that the final result is a tuple):
Finally, another usage is introduced: create a square matrix of naughn, with the same number in each row.
2. Principle analysis of zip ()
The Python pseudocode for zip () is given in the official documentation (not a built-in implementation of the Python interpreter, just to demonstrate basic code logic):
Def zip (* iterables): # zip ('ABCD',' xy')-> Ax By sentinel = object () iterators = [iter (it) for it in iterables] while iterators: result = [] for it in iterators: elem = next (it, sentinel) if elem is sentinel: return result.append (elem) yield tuple (result)
In this short code, you can analyze several key pieces of information:
Zip receives a variable number of iterable object parameters, which are processed into iterators by iter (). Corollary: if a non-iterable object occurs, an error will be reported here
The while loop determines whether the list is empty, and the elements in the list are iterators that convert parameters. Corollary: if there is a valid iterable object in the input parameter, the while loop is always true; if there is no input parameter, nothing is done.
Next () reads the next element in the iterator in turn, and its second parameter serves as the return value when the iterator is exhausted. Corollary: each round takes out an element of these iterators in turn, and when an iteration is exhausted, it exits the dead loop, which means that the unexhausted iterator will be discarded directly.
3. The problem and solution of zip ()
The most obvious problem with zip () is that it discards unexhausted iterators:
This is a barrel effect, and the final result is determined by the shortest plank.
One solution is to take the long board and make up the short board (filled with None values). This is the zip_longest method in itertools:
It is filled with redundant data and ensures the integrity of the original data to the maximum extent.
But what if we don't want redundant data, we just want data that is aligned in the longest way?
Python officials recently adopted PEP-618, which is to deal with this problem. When there is an inconsistency in the length of the iterator, it throws a ValueError instead of compromising neither the short board nor the long board. It thinks that the value of the input parameter is wrong, that is, the data integrity of the input parameter is strictly required.
Thank you for your reading, the above is the content of "how to master all the contents of Python built-in zip ()". After the study of this article, I believe you have a deeper understanding of how to master all the contents of Python built-in zip (). The specific use of the situation also 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.