python - Jupyter iPython Notebook and Command Line yield different results -
i have following python 2.7 code:
def average_rows2(mat): ''' input: 2 dimensional list of integers (matrix) output: list of floats use map take average of each row in matrix , return list. example: >>> average_rows2([[4, 5, 2, 8], [3, 9, 6, 7]]) [4.75, 6.25] ''' return map(lambda x: sum(x)/float(len(x)), mat)
when run in browser using ipython notebook, following output:
[4.75, 6.25]
however, when run code's file on command line (windows), following error:
>python -m doctest delete.py ********************************************************************** file "c:\delete.py", line 10, in delete.average_rows2 failed example: average_rows2([[4, 5, 2, 8], [3, 9, 6, 7]]) expected: [4.75, 6.25] got: <map object @ 0x00000228fe78a898> **********************************************************************
why command line toss error? there better way structure function?
it seems command line running python 3. builtin map
returns list in python 2, iterator (a map
object) in python 3. turn latter list, apply list
constructor it:
# python 2 average_rows2([[4, 5, 2, 8], [3, 9, 6, 7]]) == [4.75, 6.25] # => true # python 3 list(average_rows2([[4, 5, 2, 8], [3, 9, 6, 7]])) == [4.75, 6.25] # => true
Comments
Post a Comment