Web Images Videos Maps News Groups Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Newbie: Struggling again 'map'
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
mosscliffe  
View profile  
 More options May 26 2007, 7:54 pm
Newsgroups: comp.lang.python
From: mosscliffe <mcl.off...@googlemail.com>
Date: 26 May 2007 02:54:12 -0700
Local: Sat, May 26 2007 7:54 pm
Subject: Newbie: Struggling again 'map'
I thought I had the difference between 'zip' and 'map' sorted but when
I try to fill missing entries with something other than 'None'. I do
not seem to be able to get it to work - any pointers appreciated.

Richard

lista = ['a1', 'a2']
listb = ['b10', 'b11','b12' ,'b13']

for x,y in zip(lista, listb):    #  Fine Truncates as expected
    print "ZIP:", x, "<<x  y>>", y

for x,y in map(None, lista, listb):  #  Also fine - extends as
expected
    print "MAP:", x, "<<x  y>>", y

for x,y in map("N/A", lista, listb): ########## Fails - Can not call a
'str'
    print "MAP:", x, "<<x  y>>", y

def fillwith(fillchars):
    return fillchars

for x,y in map(fillwith("N/A"), lista, listb): ########## Fails also -
Can not call a 'str'
    print "MAP:", x, "<<x  y>>", y


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dan Bishop  
View profile  
 More options May 26 2007, 8:21 pm
Newsgroups: comp.lang.python
From: Dan Bishop <danb...@yahoo.com>
Date: 26 May 2007 03:21:16 -0700
Local: Sat, May 26 2007 8:21 pm
Subject: Re: Newbie: Struggling again 'map'
On May 26, 4:54 am, mosscliffe <mcl.off...@googlemail.com> wrote:

zip(lista + ['N/A'] * 2, listb)

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marc 'BlackJack' Rintsch  
View profile  
 More options May 26 2007, 9:34 pm
Newsgroups: comp.lang.python
From: Marc 'BlackJack' Rintsch <bj_...@gmx.net>
Date: Sat, 26 May 2007 13:34:32 +0200
Local: Sat, May 26 2007 9:34 pm
Subject: Re: Newbie: Struggling again 'map'
In <1180173252.906992.262...@q75g2000hsh.googlegroups.com>, mosscliffe
wrote:

> for x,y in map(None, lista, listb):  #  Also fine - extends as
> expected
>     print "MAP:", x, "<<x  y>>", y

> for x,y in map("N/A", lista, listb): ########## Fails - Can not call a
> 'str'
>     print "MAP:", x, "<<x  y>>", y

> def fillwith(fillchars):
>     return fillchars

> for x,y in map(fillwith("N/A"), lista, listb): ########## Fails also -
> Can not call a 'str'
>     print "MAP:", x, "<<x  y>>", y

`map()` expects a function as first argument that will be applied to the
elements in the other the arguments which have to be iterable.  That you
can give `None` as a function and the resulting behavior is IMHO a very
ugly thing and has not much to to with the semantics expected from a
`map()` function.  The `None` is not the default fill value but a
placeholder for the identity function.

Ciao,
        Marc 'BlackJack' Rintsch


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roel Schroeven  
View profile  
 More options May 26 2007, 9:47 pm
Newsgroups: comp.lang.python
From: Roel Schroeven <rschroev_nospam...@fastmail.fm>
Date: Sat, 26 May 2007 11:47:58 GMT
Local: Sat, May 26 2007 9:47 pm
Subject: Re: Newbie: Struggling again 'map'
mosscliffe schreef:

> for x,y in map("N/A", lista, listb): ########## Fails - Can not call a
> 'str'
>     print "MAP:", x, "<<x  y>>", y

> def fillwith(fillchars):
>     return fillchars

> for x,y in map(fillwith("N/A"), lista, listb): ########## Fails also -
> Can not call a 'str'
>     print "MAP:", x, "<<x  y>>", y

The first argument to map is a function, which is called with the items
of the argument sequences. If the first argument is None, a default
function is used which returns a tuple of the items. In the case that
two input sequences are provided:

map(None, lista, listb)

is equivalent to:

def maketuple(a, b):
     return a, b
map(maketuple, lista, listb)

So what you want to do can be done with map like this:

def make_fill_missing(fillchars):
     def fill_missing(a, b):
         if a is None:
             a = fillchars
         if b is None:
             b = fillchars
         return a, b
     return fill_missing

map(make_fill_missing("N/A"), lista, listb))

--
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mosscliffe  
View profile  
 More options May 26 2007, 10:15 pm
Newsgroups: comp.lang.python
From: mosscliffe <mcl.off...@googlemail.com>
Date: 26 May 2007 05:15:43 -0700
Local: Sat, May 26 2007 10:15 pm
Subject: Re: Newbie: Struggling again 'map'
Thank you all very much.

I particularily found Roel's explanation and example most useful.

At this stage I am getting my head around syntax, rather than language
theory, although I know, I have to understand that as well.

Thanks again.

Richard

On 26 May, 12:47, Roel Schroeven <rschroev_nospam...@fastmail.fm>
wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
George Sakkis  
View profile  
 More options May 27 2007, 3:10 am
Newsgroups: comp.lang.python
From: George Sakkis <george.sak...@gmail.com>
Date: 26 May 2007 10:10:11 -0700
Local: Sun, May 27 2007 3:10 am
Subject: Re: Newbie: Struggling again 'map'
On May 26, 7:47 am, Roel Schroeven <rschroev_nospam...@fastmail.fm>
wrote:

And here's a generalized iterator-based version:

def ifill(default, *iterables):
    from itertools import repeat
    nextfuncs = [iter(iterable).next for iterable in iterables]
    # how many non-exhausted iterators are left
    num_left = [len(iterables)]
    # closure for iterating over the next value of each iterator
    def iter_next_tuple_values():
        for i,next in enumerate(nextfuncs):
            try: yield next()
            except StopIteration:
                num_left[0] -= 1
                nextfuncs[i] = next = repeat(default).next
                yield next()
    while True:
        t = tuple(iter_next_tuple_values())
        if not num_left[0]:
            break
        yield t

# example
lista = ['a1', 'a2']
listb = ['b10', 'b11', 'b12', 'b13']

for iterables in [
    (lista, listb),
    (lista, listb, ()),
    ((), listb, ()),
    ((), (), ())
]:
    print list(ifill(None, *iterables)) == map(None, *iterables)

George


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7stud  
View profile  
 More options May 27 2007, 10:32 am
Newsgroups: comp.lang.python
From: 7stud <bbxx789_0...@yahoo.com>
Date: 26 May 2007 17:32:44 -0700
Local: Sun, May 27 2007 10:32 am
Subject: Re: Newbie: Struggling again 'map'
1) If you write (...) after a function name, it executes the
function(except when defining a function).  And when you write (...)
after a function name it's known as a "function call":

def calc():
   return 3.5

result = calc() + 2

2) Function calls are replaced in the code by the function's return
value:

result = calc() + 2

becomes:

result = 3.5 + 2

3) map() and zip() perform two different tasks. zip() takes two(or
more) sequences, and it returns a list of tuples, where each tuple
consists of one element from each of the sequences:

s1 = [1, 2, 3]
s2 = [10, 20, 30, 40]

print zip(s1, s2)
--->[(1, 10), (2, 20), (3, 30)]

If one sequence is shorter than the other, zip() stops when it reaches
the end of the shorter sequence.

On the other hand, map() applies a given function to each member of a
sequence and returns a list that contains the return values of the
function:

s1 = [1, 2, 3]

def f(x):
    return x*2

result = map(f, s1)
print result
---->[2, 4, 6]

If you call map() with a function and two sequences, e.g.:

map(f, s1, s2)

then the specified function will be called with two arguments--using
one element from each sequence for the arguments:

s1 = [1, 2, 3]
s2 = [10, 20, 30]

def f(x,y):
    return x + y

result = map(f, s1, s2)
print result
----->[11, 22, 33]

If one sequence is shorter than the other, then unlike zip() which
stops at the end of the shorter sequence, map() continues on until it
reaches the end of the longer sequence.  In that case, since the
shorter sequence won't have any more values to provide, map() uses
None.  In other words, map() calls the specified function with one
argument from the longer sequence and the other argument being None:

s1 = [1, 2, 3]
s2 = [10, 20, 30, 40, 50]

def f(x,y):
    return x + y

result = map(f, s1, s2)
print result
Traceback (most recent call last):
  File "2pythontest.py", line 7, in ?
    result = map(f, s1, s2)
  File "2pythontest.py", line 5, in f
    return x + y
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

The error results from the attempt inside the function f to add the
value 40 from the second sequence to None(which is used in lieu of a
value from the first sequence).  So if you want to use map() with
sequences of different lengths, before you perform any calculations in
the specified function, you have to first check to see if one of the
values is None.  If one of the values is None, then you have to take
some special action:

s1 = [1, 2, 3]
s2 = [10, 20, 30, 40, 50]

def f(x,y):
    if x==None or y==None:
        return "N/A"
    return x + y

result = map(f, s1, s2)
print result
---->[11, 22, 33, 'N/A', 'N/A']

> for x,y in map(None, lista, listb):  #  Also fine - extends as
> expected
>     print "MAP:", x, "<<x  y>>", y

That is not expected at all--at least not by me.  You have to decipher
the fine print of the map() description to expect that result.  My
expectation is the code will fail since None is not a function, and
the first argument to map() is supposed to be a function.   In any
case, that should be considered aberrant behavior--not the normal way
map() works.

> for x,y in map("N/A", lista, listb): ########## Fails - Can not call a
> 'str'
>     print "MAP:", x, "<<x  y>>", y

That is as expected since "N/A" is not a function.  After all, how can
you call a string?

s = "hello world"
print s(10)

Obviously, that's nonsensical.

> def fillwith(fillchars):
>     return fillchars

> for x,y in map(fillwith("N/A"), lista, listb): ########## Fails also -
> Can not call a 'str'

In the last line of code, the function call is replaced in the code by
the function's return value(see point 2 above--at the very top of the
post).  Since the return value of the function call fillwith("N/A") is
"N/A", the last line of your code becomes:

for x,y in map("N/A", lista, listb)

and once again map() is unable to call a string:

s = "N/A"
print s(lista[0], listb[0])

In conclusion,

zip() returns a list of tuples (where each tuple contains one element
from each sequence).

map() returns a list (where each element of the list is the return
value of a function).


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google