![]() ![]() ![]() 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. Difference between zip and izip python how to#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 |