Python - issue with using a list of frozenset entries in a for loop

1.3k Views Asked by At

I am trying to learn the apriori machine learning algorithm from a book that uses Python, and as part of that learning, I am currently stuck with this following problem:

The following code construct seems to work fine:

Ck = [[1], [2], [3], [4], [5]]
    for tranid in range(10): 
        for candidate in Ck:
            print("Printing candidate value: ", candidate)

However, the following does not work:

Ck = [[1], [2], [3], [4], [5]]
Ck2 = map(frozenset, Ck)
    for tranid in range(10): 
        for candidate in Ck2:
            print("Printing candidate value: ", candidate)

When I map every element of my original iterable to a frozenset, I notice that the inner loop ("for candidate in Ck2") executes only once. After that it never executes. The code above without the frozenset properly loops through the inner loop 10 times. However, with frozenset mapped, I can get the inner loop to execute only once.

Please help me with fixing this. The book has mapped the iterable values to frozenset because they don't want it to be mutable for the purposes of the algorithm. I am simply trying to follow it as is.

I am using Python 3.5.1 on Anaconda (Spyder).

Please help, as I am new to both Python and Machine Learning.

Thanks and Regards, Mahesh.

2

There are 2 best solutions below

0
On

The map operator does not return a list in python3 which you can iterate repeatily, but a one-time-iterable iterator. In python3.x, map works similar to itertools.imap in python2.x.

To solve the issue, use

 Ck2=list(map(frozenset, Ck)))

and see Getting a map() to return a list in Python 3.x for more information and other solutions.

0
On

In python2.x, map function returns a list. In python3.x map function return a map object, an iterable object. Indeed, a iterator. When you run the inner for loop for one times, the iterator will end so you can't get the value from it anymore. In python 2.x, you can get values from a list for many times.

You should revise it to:

Ck = [[1], [2], [3], [4], [5]]
for tranid in range(10): 
    Ck2 = map(frozenset, Ck)
    for candidate in Ck2:
        print("Printing candidate value: ", candidate)