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

python sum() 導(dǎo)入numpy后結(jié)果不同

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

問題描述

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

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 函數(shù)計算迭代的總和,然后從總和中減去第二個 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.

在第二種情況下,在導(dǎo)入 numpy 后,函數(shù)的行為似乎發(fā)生了變化,因為第二個 arg 用于指定執(zhí)行求和的軸.

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.

練習(xí)編號 (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 函數(shù),它只是陰影 __builtins__.sum,因為 from ... import * 語句將導(dǎo)入模塊中定義的所有名稱(以下劃線開頭的名稱除外)綁定到您當(dāng)前的 global 命名空間.并且根據(jù) Python 的名稱解析規(guī)則(非官方 LEGB 規(guī)則),global 命名空間在 __builtins__ 命名空間之前查找.因此,如果 Python 找到所需的名稱,在您的情況下為 sum,它會返回綁定的對象并且不會進(jìn)一步查找.

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.

編輯:向您展示發(fā)生了什么:

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 不會刪除對象,它是垃圾收集器的任務(wù),它只是取消引用"名稱綁定并從當(dāng)前命名空間中刪除名稱.

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.

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

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

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) 用于內(nèi)置 sum 總和以 -1 開頭.所以從技術(shù)上講,你的短語 iterable 的總和,然后從總和中減去第二個 args 值 是不正確的.對于數(shù)字,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 :)

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

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 成人国产网站 | 成人在线播放网站 | h视频在线观看免费 | 欧美性一级 | 91电影 | 一区二区三区视频在线观看 | 成人性生交大片免费看中文带字幕 | 日韩精品一区二区三区中文在线 | 无码日韩精品一区二区免费 | 国产亚洲网站 | 国产女人与拘做受视频 | 亚洲国产精品99久久久久久久久 | 国产做爰 | 精品免费视频 | 日韩成人免费av | 91中文视频 | 日韩中文字幕免费在线观看 | 国产在线播放av | 91网站在线播放 | 91网在线播放 | 久久国产一区 | 99精品视频在线观看 | 五月天综合影院 | 午夜丰满少妇一级毛片 | 国产精品精品久久久 | 日韩精品在线观看一区二区 | 成人毛片在线观看 | 国产福利免费视频 | 奇色影视 | 亚洲网站免费看 | 日本一区二区高清不卡 | www国产亚洲精品久久网站 | 午夜免费网 | 日韩一区不卡 | 成人av在线大片 | 日本 欧美 国产 | 欧美在线综合 | 欧美日本免费 | 精品一区二区三区日本 | 91大神在线资源观看无广告 | 91精品国产91久久久久久最新 |