![]() When you specify None as the mapping function, imap() returns the items in the iterables as tuples. print ''.join(itertools.imap(lambda x, y : chr(x) + chr(y),, )) In the following example, we include characters from the lowercase set too. You can pass multiple iterables to imap(). **DO NOT* use to generate a password as it lacks sufficient entropy. You can use this to generate a random-looking id (maybe a filename or customer id or something). print ''.join(itertools.imap(lambda x : chr(x), )) First a random number in the correct range (65, 90) is generated and imapped to a character and finally joined. Here is an example to generate a random string. Like the python global function map(), itertools provides imap() which works with iterables. For example, suppose you have a generator and you want to obtain slices from it.Ĭonsider the following fibonacci generator from which we obtain a few values using islice(). Fibonacci GeneratorĪnother difference is that islice() accepts an iterable while the slice syntax requires an array. ![]() What is the difference between the array slice syntax and islice()? Well, the slice syntax allows you to select a single slice, whereas islice() allows you to loop over multiple slices in an ordered fashion. ![]() Difference between Array Slice Syntax and islice() Print list(itertools.islice(a, 4, None, 2))ģ.1. In the following example, islice() is used to select elements starting with fourth, skipping alternate items. print list(itertools.islice(string.ascii_lowercase, 0, None, 3)) The following example extracts every third character from the alphabet. The next function in the collection is itertools.islice() which returns an iterator for looping over slices in some ordered fashion. While this might not matter for small lists, it makes a difference when processing a large number of items. Well, itertools.ifilter() returns an iterator while filter() returns a list. So what is the difference between ifilter() and filter()? Python has a global function called filter() which also filters items in a list through a predicate function. Difference between ifilter() and filter() print 'randoms div 3: ', list(itertools.ifilter(lambda n : not n % 3, ))Ģ.1. Or some random numbers divisible by 3? You get the picture. Select a few random even integers? print 'random evens: ', list(itertools.ifilter(lambda n : n % 2 = 0, )) print list(itertools.ifilter(lambda c : c > 'e', 'abcdefg')) In this article, we present a few examples of ifilter(), islice(), imap() and izip().įirst off, we have itertools.ifilter() which filters an iterable or a list for items for which a predicate function returns true.Ĭonsider this simple example which selects all characters with a numeric value greater than ‘e’. We have covered count(), cycle() and chain() in the first part of this series, and compress(), dropwhile(), and groupby() in the second part. Python provides the itertools package which provides convenience functions for many common iterator operations.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |