Re: What is not working with my "map" usage?

2018-09-22 Thread Victor via Python-list
Let me use a different input args and display them below.  Basically, I am 
hoping to add up all elements of each nested list.  So at first it should start 
with [1,11,111] ==> 1+11+111 = 123.  But instead, it appears to take the 1st 
element from each nested list to add up [1,2,3] = 6.   How should it be 
corrected?  Thx.


>>> alist = [[1,11,111], [2,22,222], [3,33,333]]

>>> list(map(add_all_elements,*alist))
My args =  (1, 2, 3)

i = 1
BEFORE total = 0
AFTER total = 1


i = 2
BEFORE total = 1
AFTER total = 3


i = 3
BEFORE total = 3
AFTER total = 6

FINAL total = 6

My args =  (11, 22, 33)

i = 11
BEFORE total = 0
AFTER total = 11


i = 22
BEFORE total = 11
AFTER total = 33


i = 33
BEFORE total = 33
AFTER total = 66

FINAL total = 66

My args =  (111, 222, 333)

i = 111
BEFORE total = 0
AFTER total = 111


i = 222
BEFORE total = 111
AFTER total = 333


i = 333
BEFORE total = 333
AFTER total = 666

FINAL total = 666

[6, 66, 666]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is not working with my "map" usage?

2018-09-22 Thread Victor via Python-list
On Saturday, September 22, 2018 at 6:22:32 AM UTC-7, Peter Otten wrote:
> Victor via Python-list wrote:
> 
> > Let me use a different input args and display them below.  Basically, I am
> > hoping to add up all elements of each nested list.  So at first it should
> > start with [1,11,111] ==> 1+11+111 = 123.  But instead, it appears to take
> > the 1st element from each nested list to add up [1,2,3] = 6.   How should
> > it be corrected?  Thx.
> 
> I see three options. You can
> 
> (1) use a list comprehension
> 
> [add_all_elements(*sub) for sub in alist]
> 
> (2) replace map() with itertools.starmap()
> 
> list(itertools.starmap(add_all_elements, alist))
> 
> (3) change your function's signature from add_all_elements(*args) to 
> add_all_elements(args), either by modifying it directly or by wrapping it 
> into another function
> 
> list(map(lambda args: add_all_elements(*args), alist))
> 
> (3a) My personal preference would be to change the signature and then use 
> the list comprehension
> 
> def add_all_elements(args): ...
> [add_all_elements(sub) for sub in alist]

Hi Peter,
Thank you for your suggested solutions.  They all work.  But I just want to 
know what is wrong with my doing:

list(map(add_all_elements,*alist))

Theoretically, each list element is passed to add_all_elements.  And if my 
alist is [[1, 11, 111], [2, 22, 222], [3, 33, 333]], then the 1st list element  
must be this [1,11,111] passed as args into add_all_elements.  

In other words, the following should have happened:  

>>> add_all_elements (*[1,11,111])
My args =  (1, 11, 111)

i = 1
BEFORE total = 0
AFTER total = 1

i = 11
BEFORE total = 1
AFTER total = 12

i = 111
BEFORE total = 12
AFTER total = 123

FINAL total = 123

123

Again, thanks!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is not working with my "map" usage?

2018-09-22 Thread Victor via Python-list
On Saturday, September 22, 2018 at 12:20:08 PM UTC-7, Thomas Jollans wrote:
> On 22/09/2018 20:18, Victor via Python-list wrote:
> > On Saturday, September 22, 2018 at 6:22:32 AM UTC-7, Peter Otten wrote:
> >> Victor via Python-list wrote:
> >>
> >>> Let me use a different input args and display them below.  Basically, I am
> >>> hoping to add up all elements of each nested list.  So at first it should
> >>> start with [1,11,111] ==> 1+11+111 = 123.  But instead, it appears to take
> >>> the 1st element from each nested list to add up [1,2,3] = 6.   How should
> >>> it be corrected?  Thx.
> >>
> >> I see three options. You can
> >>
> >> (1) use a list comprehension
> >>
> >> [add_all_elements(*sub) for sub in alist]
> >>
> >> (2) replace map() with itertools.starmap()
> >>
> >> list(itertools.starmap(add_all_elements, alist))
> >>
> >> (3) change your function's signature from add_all_elements(*args) to
> >> add_all_elements(args), either by modifying it directly or by wrapping it
> >> into another function
> >>
> >> list(map(lambda args: add_all_elements(*args), alist))
> >>
> >> (3a) My personal preference would be to change the signature and then use
> >> the list comprehension
> >>
> >> def add_all_elements(args): ...
> >> [add_all_elements(sub) for sub in alist]
> > 
> > Hi Peter,
> > Thank you for your suggested solutions.  They all work.  But I just want to 
> > know what is wrong with my doing:
> > 
> > list(map(add_all_elements,*alist))
> > 
> > Theoretically, each list element is passed to add_all_elements.  And if my 
> > alist is [[1, 11, 111], [2, 22, 222], [3, 33, 333]], then the 1st list 
> > element  must be this [1,11,111] passed as args into add_all_elements.
> 
> Now,
> 
>alist = [[1,11,111], [2,22,222], [3,33,333]]
> 
> so `map(add_all_alements, *alist)` is equivalent to
> 
>map(add_all_elements,
>[1,11,111],
>[2,22,222],
>[3,33,333])
> 
> According to the docs [1], map(function, iterable, ...)
> 
> "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."
> 
> So map takes the first item(s) of the argument(s), and applies the 
> function to them, followed by the second item(s), and so on.
> 
> In other words:
> 
> def map(function, *iterables):
>  for args in zip(iterables):
>  yield function(*args)
> 
> 
> [1] https://docs.python.org/3/library/functions.html#map
> 
> Come to think of it, this suggests a rather silly alternative "solution":
> 
> map(add_all_elements, *zip(*alist))
> 
> > 
> > In other words, the following should have happened:
> > 
> >>>> add_all_elements (*[1,11,111])
> > My args =  (1, 11, 111)
> > 
> > i = 1
> > BEFORE total = 0
> > AFTER total = 1
> > 
> > i = 11
> > BEFORE total = 1
> > AFTER total = 12
> > 
> > i = 111
> > BEFORE total = 12
> > AFTER total = 123
> > 
> > FINAL total = 123
> > 
> > 123
> > 
> > Again, thanks!
> >

Thanks all, I got it now!
-- 
https://mail.python.org/mailman/listinfo/python-list