![]() ![]() First we take the sequence and create an iterator - this allows us to keep our position as we peel off sublists, we only need to go through the list twice, no need to start from the beginning every time we loop. This version is almost as compact, but there’s a lot going on here: let’s break it down piece by piece. What we really want to do is use the compress function we’ve already written to help, since we know it’s fast. That’s much worse, since we’re doing all this extra work and throwing it away. > i = time.clock() q = pack2(long) print time.clock()-i So we can just take the second half of each tuple: def pack2(l): That’s not too good, can we do the same thing as with compress and move all of the work into a function from itertools? Here’s an implementation using the groupby function, which returns a tuple containing firstly the value of each item in the list ignoring duplicates (like compress) and a list containing all the consecutive duplicates (like pack). > i = time.clock() q = pack(long) print time.clock()-i That works fine enough, let’s see how quick it is. A first attempt would look something like: def pack(seq): One method of implementing this function could be to loop through the values, keeping track of the previous values and adding to sublists if the last was equal to the current. It is! It’s quicker, it’s clearer and it uses less memory.Ĭan we apply this approach to another kind of function, such as pack? Pack is a similar function that takes a list, and breaks consecutive runs of elements into sub lists. > i = time.clock() q = compress3(long) print time.clock() - i Using izip we can write compress like so: from itertools import * ![]() We could write a generator that works like the zip function, but why do that when Python gives us one in the itertools module: izip(). What we really want to do is generate the list of pairs piece by piece. ![]() Much worse - building that list of pairs is clearly very expensive, and you can see why, that list of two million items is now two million pairs of items. > i = time.clock() q = compress2(long) print time.clock() - i It’s definitely shorter, and arguably cleaner, but how does it go performance wise? Then we include the first item in each pair so long as it is different from the second. The trick here is that we create a list of pairs, each containing two consecutive elements in the list. Our second compress function looks like this: def compress2(x): If you’re not familiar with zip, here it is in action: > zip(, ) Let’s try to do the same thing but using a list comprehension and the built in zip() function. It doesn’t seem very pythonic though, something should tell you that there’s a better way to do things. It takes a little over two seconds on my machine, not bad really. > i = time.clock() q = compress(long) print time.clock() - i įirst we build a list of two million random elements between 0 and 50, then we check the clock before and after compressing it. Let’s quickly test the performance by trying out our compress function on a big list of numbers: NET and be confident it’s the best way to do it. It’s a fairly standard iterative solution, the kind you could write quite happily in Java or. An initial attempt might look something like: def compress(seq): Let’s start with a function that simply removes all consecutive duplicates in a list. We’ll look at some of the functions provided by the module, and work on writing small functions to compress and pack lists. ![]() With the itertools module you can quickly and simply perform some of the more complicated operations you’ll need to do on lists. We’ve gone through the basics of using list comprehensions, and using generators to iterate over a sequence without calculating it all at once. We’ll show you how with some examples using the itertools module. It will make your code perform better and become easier to read.Īfter you’ve got more Python under your belt, you can start digging into the Python standard libraries for ideas on how to fine tune your functions. With Python's itertools module you can quickly and simply perform some of the more complicated operations you'll need to do on lists. Faster, smaller, clearer: Python iterator tools ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |