Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python returning `<itertools.combinations object at 0x10049b470>` - How can I access this?

Tags:

python

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) 
like image 460
tshauck Avatar asked Mar 03 '11 03:03

tshauck


People also ask

What does Itertools combinations return?

What does itertools. combinations() do ? It returns r length subsequences of elements from the input iterable. Combinations are emitted in lexicographic sort order.

What is Itertools permutation in Python?

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.

Do you have to import Itertools?

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.


1 Answers

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.

like image 167
kennytm Avatar answered Sep 21 '22 20:09

kennytm