I have this simple piece of code that returns what's in the title. Why doesn't the array simply print? This is not just an itertools
issue I've also noticed it for other code where it'll just return the object location.
Here is the code. I'm running 2.7.1, an enthought distribution (pylab) - using it for class.
import itertools number = [53, 64, 68, 71, 77, 82, 85] print itertools.combinations(number, 4)
What does itertools. combinations() do ? It returns r length subsequences of elements from the input iterable. Combinations are emitted in lexicographic sort order.
Itertool is a module provided by Python for creating iterators for efficient looping. It also provides various features or functions that work with iterators to produce complex iterators and help us to solve problems easily and efficiently in terms of time as well as memory.
We must import the itertools module before we can use it. We will also import the operator module. This module is not necessary when using itertools , it is only needed for some of the examples below.
It doesn't print a simple list because the returned object is not a list. Apply the list
function on it if you really need a list.
print list(itertools.combinations(number, 4))
itertools.combinations
returns an iterator. An iterator is something that you can apply for
on. Usually, elements of an iterator is computed as soon as you fetch it, so there is no penalty of copying all the content to memory, unlike a list
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With