Count () function in python

Detailed explanation of the count () function https://blog.csdn.net/JNingWei/article/details/78308815

Reprinted the count () function, so that I can better refer to it later.

When I was writing the code, I wanted to get the number of List, and found that I should use len () instead of Count () directly in C #, so take a note!

Reprint

the number of times a character in string

str.count(sub, start= 0,end=len(string))

 

Args Annotations
sub Search substring
start The position where the string starts searching. The default is the first character, and the index value of the first character is 0.
end The end of the search in the string. The index of the first character in the character is 0. The default is the last position of the string.

the number of times for an element in list

list.count(obj)

Args Annotations
obj Searched list

Experimental code

the number of times a character in string


  
  
  1. # coding=utf-8
  2. string = 'Hello World ! Hello Python !'
  3. print "string.count(sub) : ", string.count( 'H')
  4. print "string.count(sub, 1) : ", string.count( 'H', 1)
  5. print "string.count (sub, 1, 100):" , string.count ( 'H' , 1 , 100 ) # Just take an infinite end parameter

Print the result:

string.count(sub) :  2
string.count(sub, 1) :  1
string.count(sub, 1, 100) :  1

the number of times for an element in list


  
  
  1. list = [ 10, 20, 30, 'Hello', 10, 20]
  2. print "list.count('Hello') : ", list.count( 'Hello')
  3. print "list.count(10) : ", list.count( 10)

Print the result:

list.count('Hello') :  1
list.count(10) :  2

The pandas value_counts () function can count and sort each value in the Series.

There is a DataFrame

If we want to know how many times each area appears, it can be as simple as:

Each area is counted and sorted in descending order from highest to lowest by default.

If you want to sort in ascending order, you can add the parameter ascending = True:

If you want to get the percentage of the count, you can add the parameter normalize = True:

Null values ​​are removed by default. The result returned by value_counts () is a Series array, which can be operated with other arrays.

value_count () is very similar to the count in the pivot table (pandas or excel), it returns a set of unique values ​​and counts. This can quickly find repeated values.

size () and shape () are functions only in the numpy module

size (): Calculate the number of all data in the array and matrix 
a = np.array ([[1,2,3], [4,5,6]]) 
np.size (a), the return value is 6 
np. size (a, 1), the return value is 3

shape (): get the size of each dimension of the matrix 
np. shape (a), the return value is (2,3)

In addition, it should be noted that shape and size can be used as a function or as a property of 
ndarray. The return value is 6, 
a.shape, and the return value is (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>
Published 8 original articles · won 16 · 40,000+ views

Detailed explanation of the count () function https://blog.csdn.net/JNingWei/article/details/78308815

Guess you like

Origin blog.csdn.net/knkn123/article/details/85122464