#小智的智商從去年的100分提升到今年的132分,請計算小智智商提升的百分比,并用字符串格式化顯示出“xx.x%”的形式,保留一位小數(shù)
十余年的集寧網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整集寧建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“集寧網(wǎng)站設(shè)計”,“集寧網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
lastYearIQ?=?100
thisYearIQ?=?132
growthRateIQ?=?(thisYearIQ-lastYearIQ)/lastYearIQ
print('小智智商今年比去年提高了%.1f%%'%(growthRateIQ*100))
#輸出:小智智商今年比去年提高了32.0%
1. 你可能會喜歡SciPy的統(tǒng)計軟件包。它有百分函數(shù)你之后,許多其他統(tǒng)計好吃的東西。
此票證相信他們不會被整合percentile()到numpy的很快。
2.
順便說一句,有百分函數(shù)的純Python,萬一一個不希望依賴于SciPy的。具體函數(shù)如下復(fù)制:
## {{{ CodeGo.net (r1)
import math
import functools
def percentile(N, percent, key=lambda x:x):
"""
Find the percentile of a list of values.
@parameter N - is a list of values. Note N MUST BE already sorted.
@parameter percent - a float value from 0.0 to 1.0.
@parameter key - optional key function to compute value from each element of N.
@return - the percentile of the values
"""
if not N:
return None
k = (len(N)-1) * percent
f = math.floor(k)
c = math.ceil(k)
if f == c:
return key(N[int(k)])
d0 = key(N[int(f)]) * (c-k)
d1 = key(N[int(c)]) * (k-f)
return d0+d1
# median is 50th percentile.
median = functools.partial(percentile, percent=0.5)
## end of CodeGo.net }}}
3.
檢查scipy.stats模塊:
scipy.stats.scoreatpercentile
4.
import numpy as np
a = [154, 400, 1124, 82, 94, 108]
print np.percentile(a,95) # gives the 95th percentile
5.
百分看到定義預(yù)期結(jié)果從提供的列表,低于該值的百分之P被發(fā)現(xiàn)的價值。為了得到這一點(diǎn),你一個簡單的函數(shù)。
def percentile(N, P):
"""
Find the percentile of a list of values
@parameter N - A list of values. N must be sorted.
@parameter P - A float value from 0.0 to 1.0
@return - The percentile of the values.
"""
n = int(round(P * len(N) + 0.5))
return N[n-1]
# A = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# B = (15, 20, 35, 40, 50)
#
# print percentile(A, P=0.3)
# 4
# print percentile(A, P=0.8)
# 9
# print percentile(B, P=0.3)
# 20
# print percentile(B, P=0.8)
# 50
如果您寧愿從處于或低于該值的百分之P被發(fā)現(xiàn)所提供的列表中獲得的價值,這個簡單的修改:
def percentile(N, P):
n = int(round(P * len(N) + 0.5))
if n 1:
return N[n-2]
else:
return 0
6.
numpy.percentile
在那里我很想念?
7.
size=len(mylist)
p5=mylist[math.ceil((size*5)/100)-1]
p25=mylist[math.ceil((size*25)/100)-1]
p50=mylist[math.ceil((size*50)/100)-1]
p75=mylist[math.ceil((size*75)/100)-1]
p95=mylist[math.ceil((size*95)/100)-1]
例如輸入 10%
a=input('請輸入百分比:')
請輸入百分比:'10%'
a
方式1:參數(shù)格式化:{:.2%}、{:.1%}、{:.0%}
{:.2%}: 顯示小數(shù)點(diǎn)后2位
print('percent: {:.2%}'.format(10/50))
percent: 25.00%
print('percent: {:.1%}'.format(10/50))
percent: 25.0%
print('percent: {:.0%}'.format(10/50))
percent: 25%
方式2:先格式化為float,再處理成%格式: {:.2f}%、{:.1f}%、?{:.0f}%
print('percent: {:.2f}%'.format(10/50*100))
percent: 25.00%
print('percent: {:.0f}%'.format(10/50*100))
percent: 25%
特別說明
方式二相對于方式一,把%提到{}外,但計算值的時候必須乘以100
個人認(rèn)為,format是最好用的格式輸出方法。
利用format將" 小數(shù)轉(zhuǎn)為對應(yīng)的百分?jǐn)?shù) "輸出的操作如下:
說明:{:%}用來將小數(shù)轉(zhuǎn)換為百分?jǐn)?shù),其中的.2是保留兩位小數(shù)。所以{:.2%}就是:將小數(shù)轉(zhuǎn)為對應(yīng)的百分?jǐn)?shù),并保留兩位小數(shù)輸出。
Format為CString類的一個成員函數(shù),它通過格式操作使任意類型的數(shù)據(jù)轉(zhuǎn)換成一個字符串Format里面可以寫普通的字符串,比如“mynameis”,但有些格式指令字符具有特殊意義,比如“%6s”。
Format(表達(dá)式[,格式字符串])其中,表達(dá)式:要格式化的數(shù)值、日期或字符串表達(dá)式。格式字符串:指定表達(dá)式的值的輸出格式。格式字符有三類:數(shù)值格式、日期格式和字符串格式。格式字符要加引號。
方法如下:
1、首先按下“Win+R”組合鍵,打開運(yùn)行窗口。
2、在打開文本框輸入“cmd”,點(diǎn)擊確定。
3、在打開的cmd窗口中,輸入:“python”,點(diǎn)擊Enter鍵。
4、在Python環(huán)境中,輸入:“x = format(0.5, '%')”,點(diǎn)擊Enter鍵。
5、在Python環(huán)境中,輸入:“print(x)”。
6、點(diǎn)擊Enter鍵,即可使用Python內(nèi)置的format函數(shù)把數(shù)字0.5格式化為百分比值。
當(dāng)前題目:python百分比函數(shù) 百分比在python
URL標(biāo)題:http://jinyejixie.com/article20/hpdgco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、面包屑導(dǎo)航、小程序開發(fā)、Google、企業(yè)建站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)