萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> Python中List.count()方法的使用教程

Python中List.count()方法的使用教程

   這篇文章主要介紹了Python中List.count()方法的使用教程,是Python入門中的基礎知識,需要的朋友可以參考下

  count()方法返回obj出現在列表的次數。

  語法

  以下是count()方法的語法:

  ?

1 list.count(obj)

  參數

  obj -- 這是在該列表被計數的對象。

  返回值

  此方法返回obj出現在列表的次數。

  例子

  下面的例子顯示了count()方法的使用。

  ?

1 2 3 4 5 6 #!/usr/bin/python   aList = [123, 'xyz', 'zara', 'abc', 123];   print "Count for 123 : ", aList.count(123); print "Count for zara : ", aList.count('zara');

  當我們運行上面的程序,它會產生以下結果:

  ?

1 2 Count for 123 : 2 Count for zara : 1
copyright © 萬盛學電腦網 all rights reserved