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?

falsetru
  • 357,413
  • 63
  • 732
  • 636
skfeng
  • 669
  • 2
  • 7
  • 15
  • 4
    It return generator for 3+, Do `list(map(set, dataset))` – Grijesh Chauhan Oct 27 '13 at 09:07
  • 2
    Read [Getting a map() to return a list in python 3.1](http://stackoverflow.com/questions/1303347/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) – Grijesh Chauhan Oct 27 '13 at 09:13
  • 1
    You can also use LCs `[set(i) for i in dataset]` – Grijesh Chauhan Oct 27 '13 at 09: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. – glglgl Oct 27 '13 at 09:23

2 Answers2

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

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
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!

Community
  • 1
  • 1
aIKid
  • 26,968
  • 4
  • 39
  • 65