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/03 Report--
This article mainly shows you "how to use the sum () function to reduce the dimension of the list", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use the sum () function to reduce the dimension of the list" this article.
Last month, the S student in the study group asked a question, which can be understood as list dimensionality reduction. The example is as follows:
Oldlist = [[1,2,3], [4,5]] # want results: newlist = [1,2,3,4,5]
The raw data is a two-dimensional list that aims to get the specific values of all elements in the list. From an abstract point of view, it can also be regarded as list decompression or list dimensionality reduction.
This question is not difficult, but how to write it more elegantly?
# method 1, rough stitching: newlist = oldlist [0] + oldlist [1]
This method is simple and rough, and what content needs to be stitched together, take it out and splice it directly. However, if the original list has many sublists, this method becomes tedious.
Let's upgrade the original question: a two-dimensional list contains n one-dimensional list elements, how to gracefully put these sublists into a new one-dimensional list?
The practice of method 1 requires writing n objects and n-1 stitching operations. Of course not. Let's take a look at method two:
# method 2, list derivation: newlist = [i for j in range (len (oldlist)) for i in oldlist [j]]
There are two for statements in this expression. In the first for statement, we first take out the length of the original list, and then construct the range object, where the value range of j is the closed interval of [0, n Mel 1].
In the second for statement, oldlist [j] refers to the j sublist of the original list, while for i in oldlist [j] traverses the elements that take out the j sublist. Because the interval of j values corresponds to all the index values of the original list, the problem is finally solved.
This method is elegant enough, and it is not difficult to understand.
However, can we be satisfied with this? Are there any other tricks that are obscene and skillful, oh no, other advanced methods? Student F contributed a train of thought:
# method 3, skillfully use sum:newlist = sum (oldlist, [])
To tell you the truth, this method surprised me! Isn't the sum () function for summation? Why is it used in this way?
What principle is used in this writing? I was confused for a while because I didn't know that the sum () function could take two parameters and how they were used for calculation. However, when I knew the full use of sum (), it dawned on me.
It's not going to happen anymore, so let's just find out.
Syntax: sum (iterable [, start]), the first argument to the sum () function is an iterable object, such as a list, tuple, or collection, and the second argument is the starting value, which defaults to 0. Its purpose is to "add" all elements of an iterable object based on the start value.
In the above example, the execution effect is that the sublists in the oldlist are added to the second parameter one by one, and the addition of the list is equivalent to the extend operation, so the end result is an expanded list from [].
There are two key points here: the sum () function allows you to take two parameters, and the second parameter is the starting point. It is possible that the sum () function is often used for numerical summation, but it has a strange effect when it is used for list summation. It is more elegant and concise than list derivation!
At this point, the previous upgrade question has been well answered. In retrospect, students' initial problems can be realized in three ways: the first method is proper, the second method is right and advanced, and the third method is out of the way (not derogatory, just that it is unexpected, but it works well).
This is not a difficult question, after public discussion and sharing, but also led to a very valuable learning content. Not long ago, it was also a problem in the group, which also produced the same learning effect.
I got an inspiration from this: we should think about the problem from multiple angles and try to find a better solution. at the same time, the basic knowledge should be firmly mastered and integrated flexibly.
There is no end to learning, and here I would like to open up my mind and see what I can find.
1. If the elements of the original list have other types of elements besides the list, how do you merge the same kind of elements together?
2. If it is a three-dimensional or higher-dimensional list, how can it be better compressed into an one-dimensional list?
3. What other knowledge points are there in the sum () function?
The first two problems have increased the complexity, and there seems to be no "panacea" to solve them, so we can only disassemble them separately and decompress them one by one.
The third question is about the use of the sum () function itself. Let's take a look at what the official documentation says:
The iterable's items are normally numbers, and the start value is not allowed to be a string.
For some use cases, there are good alternatives to sum (). The preferred, fast way to concatenate a sequence of strings is by calling'. Join (sequence). To add floating point values with extended precision, see math.fsum (). To concatenate a series of iterables, consider using itertools.chain ().
The second argument to sum () is not allowed to be a string. If you use it, you will report an error:
TypeError: sum () can't sum strings [use''.join (seq) instead]
Why not recommend using sum () to concatenate strings? Haha, the join () method is recommended in the documentation because it is faster. In order not to allow us to use slow methods, it specifically limits that the second parameter that does not allow sum () is a string.
The document also recommends that you do not use sum () in some usage scenarios, such as when summing floating-point numbers with extended precision, and consider using itertools.chain () when you want to concatenate a series of iterable objects.
The above is all the contents of the article "how to use the sum () function to reduce the dimension of a list". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.