Python - problem using list of frozenset entries in for loop

I am trying to learn an a priori computer learning algorithm from a book that uses Python, and as part of this tutorial, I currently adhere to this following problem:

The following code construct works 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 match each element of my original iterable using frozenset, I notice that the inner loop ("for candidate Ck2") runs only once. After that, it is never executed. The code above without a phenisometer properly goes through the internal circuit 10 times. However, with frozenset display, I can get the inner loop to execute only once.

Please help me with this. The book displays iterable values ​​in frozenset because they do not want it to be volatile for algorithm purposes. I'm just trying to follow it as it is.

I am using Python 3.5.1 on Anaconda (Spyder).

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

Thanks and respect, Mahesh.

+5
source share
2 answers

The map operator does not return a list in python3, which you can repeat a repeating, but one-time iterator. In python3.x, map works similarly to itertools.imap in python2.x.

To solve the problem, use

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

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

+5
source

In python2.x, the map function returns a list. In the python3.x function, map returns a map object, an iterable object. Indeed, an iterator. When you run the inner for loop once, the iterator ends, so you can no longer get the value. In python 2.x, you can get values ​​from a list many times.

You should review it:

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

Source: https://habr.com/ru/post/1242759/


All Articles