![]() 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. The source of the stream, Stream elements - lines, words, ints - are processed one at a time, and nothing accumulates except the final total.Faster, smaller, clearer: Python iterator tools Each generator expression builds upon the one on the preceding line. You’ll notice how clean and declarative it is. In his presentation David Beazley shows some elegant and idiomatic Python code to sum the total number of bytes transferred in an Apache httpd server log (the final field on each line of the log file shown above). Give it a list and each application of takewhile resumes iteration from the beginning of the list, meaning take_some either repeats the first element of the list forever, or it spins without yielding anything:Īpache httpd log 81.107.39.38. I could point out a couple of bugs in take_some. So repeatedly applying takewhile to a stream drops the elements for which the predicate doesn’t hold, which is to say it generates the elements for which the predicate holds, which is of course ifilter. That is, we drop the first element for which the predicate fails. There you have it! Once a stream returned by takewhile has run its course, the original iterable is poised to yield the element immediately after the first element for which the predicate fails. Make an iterator that returns elements from the iterable as long as the predicate is true. The actual documentation for itertools.takewhile reads: ![]() Our casual interpretation of take_some was wrong. This time, as you can see, we do start spinning, and we have to interrupt execution to regain control. ![]() Here itertools.dropwhile iterates through the zipped stream yielding items as soon as it detects a difference in the first and second element of a pair. > diverge = dropwhile(lambda xy: xy = xy, izip(xs, ys))Ĭ-c C-cTraceback (most recent call last): from itertools import dropwhile, ifilter, izip ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |