久久久久久久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é)果不同的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我遇到了 Jake VanderPlas 提出的這個(gè)問題,我不確定我對(duì)導(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ù)計(jì)算迭代的總和,然后從總和中減去第二個(gè) 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ā)生了變化,因?yàn)榈诙€(gè) 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í)編號(hào) (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個(gè)迂腐幣到@Warren Weckesser 回答.真的 from numpy import * 不會(huì)覆蓋 builtins sum 函數(shù),它只是陰影 __builtins__.sum,因?yàn)?from ... import * 語句將導(dǎo)入模塊中定義的所有名稱(以下劃線開頭的名稱除外)綁定到您當(dāng)前的 global 命名空間.并且根據(jù) Python 的名稱解析規(guī)則(非官方 LEGB 規(guī)則),global 命名空間在 __builtins__ 命名空間之前查找.因此,如果 Python 找到所需的名稱,在您的情況下為 sum,它會(huì)返回綁定的對(duì)象并且不會(huì)進(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

第一個(gè)說明:del 不會(huì)刪除對(duì)象,它是垃圾收集器的任務(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.

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

startiterable 的項(xiàng)目從左到右求和并返回總數(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 的總和,然后從總和中減去第二個(gè) args 值 是不正確的.對(duì)于數(shù)字,add/subtract<開始并不重要/em> 稍后.但是對(duì)于列表它確實(shí)如此(愚蠢的例子只是為了展示這個(gè)想法):

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é)果不同的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 91欧美精品 | 国产精品久久久久久久久久 | 俺去俺来也www色官网cms | 久久久精品综合 | 久久九九色 | 国产精品观看 | 免费黄色大片 | 久久久91 | 国产精品久久久久久久久久久久久久 | 成人国产精品久久久 | 99久久久国产精品 | 欧日韩在线观看 | 精品国产不卡一区二区三区 | 国产精品99999999| 国产午夜亚洲精品不卡 | 精品一区二区三区四区在线 | 国产污视频在线 | 一区二区三区亚洲视频 | 亚洲精品99 | 亚洲国产成人在线视频 | 欧美色综合一区二区三区 | 全免费a级毛片免费看视频免 | 国产精品免费观看 | 日韩成人免费视频 | 欧美一级毛片免费观看 | www国产成人免费观看视频 | 免费国产一区二区视频 | 中文一区二区 | 亚洲精品一| 精品亚洲一区二区三区四区五区 | 欧美日韩一 | 日本三级全黄三级a | 成人久久网 | 国产精品综合色区在线观看 | 国产精品区二区三区日本 | 国产视频第一页 | 欧美在线a| 碰碰视频| 91久久电影 | 婷婷狠狠 | 久久精品这里 |