2016-08-24 9 views
1

I次のPython 2.7のコードを持っている:私はiPythonノートブックを使用して、私のブラウザでそれを実行Jupyter iPythonノートブックおよびコマンドラインの歩留まり異なる結果

def average_rows2(mat): 
    ''' 
    INPUT: 2 dimensional list of integers (matrix) 
    OUTPUT: list of floats 

    Use map to take the average of each row in the matrix and 
    return it as a 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) 

、私は次のような出力を得る:

[4.75, 6.25] 
を私は、コマンドライン(Windowsの場合)上のコードのファイルを実行すると

はしかし、私は次のエラーを取得する:

>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 at 0x00000228FE78A898> 
********************************************************************** 

コマンドラインでエラーが発生するのはなぜですか?私の機能を構成する良い方法はありますか?

答えて

5

それはあなたのコマンドラインのように思えるが、リストに後者を有効にするにはPython 3でのPythonの組み込みmapリターンのPython 2でリスト3が、イテレータ(mapオブジェクト)を実行している、とlistコンストラクタを適用それ:

# 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 
関連する問題