久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

python sum() 導入numpy后結果不同

Python sum() has a different result after importing numpy(python sum() 導入numpy后結果不同)
本文介紹了python sum() 導入numpy后結果不同的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我遇到了 Jake VanderPlas 提出的這個問題,我不確定我對導入 numpy 模塊后結果為何不同的理解是否完全正確.

I came across this problem by Jake VanderPlas and I am not sure if my understanding of why the result differs after importing the numpy module is entirely correct.

>>print(sum(range(5),-1)
>> 9
>> from numpy import *
>> print(sum(range(5),-1))
>> 10

似乎在第一種情況下,sum 函數計算迭代的總和,然后從總和中減去第二個 args 值.

It seems like in the first scenario the sum function calculates the sum over the iterable and then subtracts the second args value from the sum.

在第二種情況下,在導入 numpy 后,函數的行為似乎發生了變化,因為第二個 arg 用于指定執行求和的軸.

In the second scenario, after importing numpy, the behavior of the function seems to have modified as the second arg is used to specify the axis along which the sum should be performed.

練習編號 (24)來源 - http://www.labri.fr/perso/nrougier/teaching/numpy.100/index.html

Exercise number (24) Source - http://www.labri.fr/perso/nrougier/teaching/numpy.100/index.html

推薦答案

只添加我的5個迂腐幣到@Warren Weckesser 回答.真的 from numpy import * 不會覆蓋 builtins sum 函數,它只是陰影 __builtins__.sum,因為 from ... import * 語句將導入模塊中定義的所有名稱(以下劃線開頭的名稱除外)綁定到您當前的 global 命名空間.并且根據 Python 的名稱解析規則(非官方 LEGB 規則),global 命名空間在 __builtins__ 命名空間之前查找.因此,如果 Python 找到所需的名稱,在您的情況下為 sum,它會返回綁定的對象并且不會進一步查找.

Only to add my 5 pedantic coins to @Warren Weckesser answer. Really from numpy import * does not overwrite the builtins sum function, it only shadows __builtins__.sum, because from ... import * statement binds all names defined in the imported module, except those beginning with an underscore, to your current global namespace. And according to Python's name resolution rule (unofficialy LEGB rule), the global namespace is looked up before __builtins__ namespace. So if Python finds desired name, in your case sum, it returns you the binded object and does not look further.

編輯:向您展示發生了什么:

EDIT: To show you what is going on:

 In[1]: print(sum, ' from ', sum.__module__)    # here you see the standard `sum` function
Out[1]: <built-in function sum>  from  builtins

 In[2]: from numpy import *                     # from here it is shadowed
        print(sum, ' from ', sum.__module__)
Out[2]: <function sum at 0x00000229B30E2730>  from  numpy.core.fromnumeric

 In[3]: del sum                                 # here you restore things back
        print(sum, ' from ', sum.__module__)
Out[3]: <built-in function sum>  from  builtins

第一個說明:del 不會刪除對象,它是垃圾收集器的任務,它只是取消引用"名稱綁定并從當前命名空間中刪除名稱.

First note: del does not delete objects, it is a task of garbage collector, it only "dereference" the name-bindings and delete names from current namespace.

第二個說明:內置sum函數的簽名是sum(iterable[, start]):

startiterable 的項目從左到右求和并返回總數.start 默認為 0.iterable的item一般是數字,起始值不允許是字符串.

Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. The iterable‘s items are normally numbers, and the start value is not allowed to be a string.

我你的情況 print(sum(range(5),-1) 用于內置 sum 總和以 -1 開頭.所以從技術上講,你的短語 iterable 的總和,然后從總和中減去第二個 args 值 是不正確的.對于數字,add/subtract<開始并不重要/em> 稍后.但是對于列表它確實如此(愚蠢的例子只是為了展示這個想法):

I your case print(sum(range(5),-1) for built-in sum summation starts with -1. So technically, your phrase the sum over the iterable and then subtracts the second args value from the sum isn't correct. For numbers it's really does not matter to start with or add/subtract later. But for lists it does (silly example only to show the idea):

 In[1]: sum([[1], [2], [3]], [4])
Out[1]: [4, 1, 2, 3]               # not [1, 2, 3, 4]

希望這能澄清你的想法:)

Hope this will clarify your thoughts :)

這篇關于python sum() 導入numpy后結果不同的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 黄色录像免费看 | 亚洲精品美女 | 久久麻豆视频 | 久久久久精 | 日韩在线精品 | 国产成人三级在线观看 | 久久久久久久91 | 一区二区三区国产视频 | 成人黄色小视频 | 亚洲丝袜av| 国产黄a三级三级看三级 | 黄色欧美视频 | 欧美一级免费看 | 午夜性视频| 福利视频网站 | 不卡视频在线观看 | 免费观看av网站 | www.亚洲视频| 91性高潮久久久久久久久 | 天天爽夜夜爽 | 国产一及片 | av福利在线 | 四虎在线免费观看视频 | 欧美黑人一区二区三区 | 毛片一级片| 精品视频免费在线观看 | 一级片大全| 一区二区三区四区视频 | 一区二区三区四区精品 | 久久夜色精品国产欧美乱极品 | 国产又粗又猛又黄又爽的视频 | 日韩欧美二区 | 亚洲一级大片 | 亚洲国产成人精品女人久久久 | a级片久久| 在线观看的av网站 | 伊人久久影院 | 日韩欧美在线观看视频 | 天天摸天天操 | 欧美成人性生活视频 | 中文字幕在线视频播放 |