如何计算Python中列表项的出现次数?
The python count() method counts the number of occurrences of an element in the list and returns it.
python count()方法对列表中某个元素的出现次数进行计数并返回它。
Syntax:
句法:
list.count(x)
The count() method takes a single argument, x, whose count is to be found. The count() method returns the number of occurrences of an element in a list.
count()方法采用一个参数x ,该参数的计数将被找到。 count()方法返回列表中某个元素的出现次数。
Example:
例:
# an example of list.count() method in Python
# declaring a list
website_list = ['google.com','includehelp.com', 'linkedin.com', 'google.com']
# counting the occurrences of 'google.com'
count = website_list.count('google.com')
print('google.com found',count,'times.')
# counting the occurrences of 'linkedin.com'
count = website_list.count('linkedin.com')
print('linkedin.com found',count,'times.')
Output
输出量
google.com found 2 times.
linkedin.com found 1 times.
The count() method works on tuple as well
count()方法也适用于元组
Example:
例:
# an example of count() method with tuple in Python
# declaring a tuple
sample_tuple = ((1,3), (2,4), (4,6))
# conting occurrences of (1,2)
count = sample_tuple.count((1,2))
print('(1,2) found',count,'times.')
# conting occurrences of (1,3)
count = sample_tuple.count((1,3))
print('(1,3) found',count,'times.')
Output
输出量
(1,2) found 0 times.
(1,3) found 1 times.
翻译自: https://www.includehelp.com/python/how-can-i-count-the-occurrences-of-a-list-item.aspx
如何计算Python中列表项的出现次数?相关推荐
- Python中列表 list 的加减乘除运算总结
Python中列表运算总结 列表的创建及基本用法 列表的 + - × ÷ Python中列表list是一种常用(最基本)的数据类型,其四则运算和MATLAB.numpy等数据结构不太一样. 列表的创建 ...
- Python中列表及其操作
文章目录 前言 一.列表简介 二.访问列表元素 三.修改.添加和删除列表元素 1.修改列表元素 2.在列表中添加元素 3.从列表中删除元素 四.对列表元素排序 五.确定列表长度 六.遍历列表 七.创建 ...
- python列表和元组的应用,Python中列表和元组的使用方法和区别
一.二者区别 列表: 1.可以增加列表内容 append 2.可以统计某个列表段在整个列表中出现的次数 count 3.可以插入一个字符串,并把整个字符串的每个字母拆分当作一个列表段追加到列表当中 e ...
- li前面的原点或者方的样式修改html中列表项li所显示的圆点的颜色?,以及相关样式的设定...
如何修改html中列表项li所显示的圆点的颜色?,以及相关样式的设定 这是li标签自带的圆点的颜色改变,代码如下: .centerbt li{ content:"."; col ...
- Python中列表和字符串的反转
要求: 原列表:[13, 30, 42, 85, 9, 45] 反转后的列表:[45, 9, 85, 42, 30, 13] 原字符串:hello 反转后的字符串:olleh Python现成的反转功 ...
- 手写Python中列表和字符串的反转
要求: 原列表:[13, 30, 42, 85, 9, 45] 反转后的列表:[45, 9, 85, 42, 30, 13] 原字符串:hello 反转后的字符串:olleh Python现成的反转功 ...
- python中列表 元组 字典 集合的区别
参考文章:python中列表 元组 字典 集合的区别
- python中列表的运用_python中列表的应用
标签: 本文主要介绍了:python中列表的主要应用和一些列表自带的一些函数 代码: #!/usr/bin/env python # author by lh # -*- coding:utf-8 - ...
- python中列表的sort方法_python中列表的sort方法使用详解
内容简介:python中列表的sort方法使用详解 一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可修改的. 排序,数字.字符 ...
最新文章
- linux 密码记录文件 .netrc 简介
- Sprint 站立会议(个人)
- 关节点(atriculation point)算法
- MagicRecord For IOS 简介
- 面试中这样自我介绍更能抓住面试官的耳朵
- 禁止吸烟(字符串替换)
- 选择排序----详细算法分析
- vs2019怎么调整字体大小_每年至少十万考生涂错答题卡,是时候讨论一下最新版答题卡怎么用了...
- iOS开发之创建颜色渐变视图View
- Windows Mobile Sensors API库的设计
- 使用“Apple 诊断”来测试您的Mac 是不是存在硬件问题
- java写txt文件_Java读写txt文件
- 7个实用的免费网站托管站点
- 数据库范式 1NF, 2NF, 3NF的问题与细解
- Linux网络包接收过程
- Elasticsearch基础11——索引之别名使用
- 超级电容容量、耐压值、电容值计算方法
- CSDN学霸课表——系统集成项目管理工程师(软考中级)
- 国家电子政务体系学习
- 互联网玄学的“造神”奇迹还能持续多久?