简体   繁体   中英

Python3.6 map object

What is the difference between the following two code snippets:

a1 = map(int,'222211112211')

and

a2 = int('222211112211')

Why can I iterate over a1 which is type int ?

For example: I can do something like this with a1 :

for i in a1:
    print(i)

But not with a2 ?

In a1 , you're iterating over a string and converting each of its characters into an int , the result is an iterator of int s, check the documentation for map :

list(a1) # use `list()` to consume the iterator
=> [2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1]

In a2 , you're converting a string into an int , the result is an int . They're very different things!

a2
=> 222211112211

That's why you can iterate over a1 , because it's an iterator, not an int . And that's why you can't iterate over a2 , it's just an int .

I think the best thing to do in situations like these is to check the actual values in the REPL.

>>> a1 = map(int, '2212')
>>> a1
<map object at 0x7f0036c2a4a8>
>>> list(a1)
[2, 2, 1, 2]
>>> a2 = int('2212')
>>> a2
2212

So a1 is a special map object which turns out to be iterable. It stores each character of '2212' , individually converted to an integer. Meanwhile, a2 simply converts the whole string to a simple integer. It would be an error to iterate over a2 , but it would also be an error to do integer arithmetic on a1 .

in python 3.6 ,the map function returns a generator ,

Note:these are are not loaded into the memory as a whole

This is the implementation of map in python3

def map(func, iterable):
    for i in iterable:
        yield func(i)

when u call the map as iterable the value is produced onspot as opposed to loading the entire thing, which could crash the memory in some cases.

list(map()) does is load the entire list into the memory which is what python2 versions of map does

`a=int('2212')` 

gives a an integer value thats it its not an iterable

a=map(int,'2212')

returns an iterable generator object

<map object at 0x7f97e30d7f98>

it is an iterable which converts each character in the string to int and yields the result one by one

so

a=map(int ,'2212')
for i in a:
   print(i)
for i in a:
   print(i)

would print

2
2
1
2

calling the stored map object second time yields no result as the function has run out of values in the first run

if you want to get the values in the second run convert it to a list so that it resides in the main memory list(a) or if it is really long store the result of the map object in a separate file and read from there

In Python 3, map return an iterator that you can use in a for loop.

map takes at least two parameters, a function and an iterable argument. Here int is the function and '222211112211' , a string, is the iterable object. map applies the function to each value of the iterable. Here, int will be applied to "2", "2", "2", ... "2", "1", "1" individually and make them all to be integers. map will return an iterator that allows you to loop over the results yielding from the previous step: (2, 2, 2, ..., 1, 1)

For a2 , you are creating an integer out of the int function, and an integer is not iterable. Thus, you cannot loop of it.

Below is the description of map cited from Python 3 documentation.

map(function, iterable, ...) : Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see itertools.starmap().

One thing worth noting is that in Python 2, map returns a list rather than an iterator. In Python 3, you have to explicitly make it a list like list(map(int, "222")) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM