1
>>> dataset
[[1, 3, 4], [2, 3, 5], [1, 2, 3, 5], [2, 5]]
>>> D=map(set,dataset)
>>> D
<map object at 0x0000000002ABF5F8>

When I input D into interactive window of python3.3,I supposed that it should appear:

[set([1, 3, 4]), set([2, 3, 5]), set([1, 2, 3, 5]), set([2, 5])]

Why a map object?

4
  • 4
    It return generator for 3+, Do list(map(set, dataset)) Commented Oct 27, 2013 at 9:07
  • 2
    Read Getting a map() to return a list in python 3.1 I didn't read anywhere But I observe the difference that in new Python version most in-built functions return generator instead of sequences (for efficiency purpose I suppose) Commented Oct 27, 2013 at 9:13
  • 1
    You can also use LCs [set(i) for i in dataset] Commented Oct 27, 2013 at 9:14
  • 1
    When noticing strange behaviour if using Py3, it is always worth to take a look at the function's documentation. There are many subtledifferences, making your life hard if you are used to Py2. Commented Oct 27, 2013 at 9:23

2 Answers 2

2

Why the result of map is an object? Because in documentation for python 3.3 it is written that it yields the result, thus it is a generator.

You can read it by using

for i in D:
  print i

or as it was suggested by Grijesh

list(D) will convert it to list

Sign up to request clarification or add additional context in comments.

Comments

2

In Python 3, map doesn't return a list, but a map object, an iterator. There's a similar question about this. If you need a list, you can easily convert it to a list:

D = list(map(set,dataset))

See the docs about this.

By the way, it's a good practice to use lower case letters for variable names. Hope this helps!

1 Comment

Haven't seen yours when i wrote it. Sorry mate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.