count()函數之詳解 https://blog.csdn.net/JNingWei/article/details/78308815
轉載了count()函數,以便自己以後更好的查閱。
自己在寫代碼時想要獲取List的個數,發現了因該使用len(),而不是C#中直接用的Count(),因此做個筆記!
轉載,侵刪
string 中 某字符 的次數
str.count(sub, start= 0,end=len(string))
Args | Annotations |
---|---|
sub | 搜索的子字符串 |
start | 字符串開始搜索的位置。默認爲第一個字符,第一個字符索引值爲0。 |
end | 字符串中結束搜索的位置。字符中第一個字符的索引爲 0。默認爲字符串的最後一個位置。 |
list 中 某元素 的次數
list.count(obj)
Args | Annotations |
---|---|
obj | 搜索的list |
實驗代碼
string 中 某字符 的次數
- # coding=utf-8
- string = 'Hello World ! Hello Python !'
- print "string.count(sub) : ", string.count('H')
- print "string.count(sub, 1) : ", string.count('H', 1)
- print "string.count(sub, 1, 100) : ", string.count('H', 1, 100) # 隨便取個 無限大的 end 參數
打印結果:
string.count(sub) : 2
string.count(sub, 1) : 1
string.count(sub, 1, 100) : 1
list 中 某元素 的次數
- list = [10, 20, 30, 'Hello', 10, 20]
- print "list.count('Hello') : ", list.count('Hello')
- print "list.count(10) : ", list.count(10)
打印結果:
list.count('Hello') : 1
list.count(10) : 2
pandas 的value_counts()函數可以對Series裏面的每個值進行計數並且排序。
現有一個DataFrame
如果我們想知道,每個區域出現了多少次,可以簡單如下:
每個區域都被計數,並且默認從最高到最低做降序排列。
如果想用升序排列,可以加參數ascending=True:
如果想得出的計數佔比,可以加參數normalize=True:
空值是默認剔除掉的。value_counts()返回的結果是一個Series數組,可以跟別的數組進行運算。
value_count()跟透視表裏(pandas或者excel)的計數很相似,都是返回一組唯一值,並進行計數。這樣能快速找出重複出現的值。
size()和shape () 是numpy模塊中才有的函數
size():計算數組和矩陣所有數據的個數
a = np.array([[1,2,3],[4,5,6]])
np.size(a),返回值爲 6
np.size(a,1),返回值爲 3
shape ():得到矩陣每維的大小
np. shape (a),返回值爲 (2,3)
另外要注意的是,shape和size既可以作爲函數,也可以作爲ndarray的屬性
a.size,返回值爲 6,
a.shape,返回值爲 (2,3)
<script>
(function(){
function setArticleH(btnReadmore,posi){
var winH = $(window).height();
var articleBox = $("div.article_content");
var artH = articleBox.height();
if(artH > winH*posi){
articleBox.css({
'height':winH*posi+'px',
'overflow':'hidden'
})
btnReadmore.click(function(){
if(typeof window.localStorage === "object" && typeof window.csdn.anonymousUserLimit === "object"){
if(!window.csdn.anonymousUserLimit.judgment()){
window.csdn.anonymousUserLimit.Jumplogin();
return false;
}else if(!currentUserName){
window.csdn.anonymousUserLimit.updata();
}
}
articleBox.removeAttr("style");
$(this).parent().remove();
})
}else{
btnReadmore.parent().remove();
}
}
var btnReadmore = $("#btn-readmore");
if(btnReadmore.length>0){
if(currentUserName){
setArticleH(btnReadmore,3);
}else{
setArticleH(btnReadmore,1.2);
}
}
})()
</script>
</article>