Friday 4 December 2015

Python: count: Count number of times element appears

List provides count method, to count number of times element appear in the result.


test.py

list=[12, 5, 32, 43, 21, 356, 5, 21]
print(list)

print("5 appears ",list.count(5)," times")
print("12 appears ",list.count(12)," times")

$ python3 test.py
[12, 5, 32, 43, 21, 356, 5, 21]
5 appears  2  times
12 appears  1  times



Previous                                                 Next                                                 Home

No comments:

Post a Comment