如何计算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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545522.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

网页自动关闭的代码

<script language"javascript">window.openernull; window.open(, _self, );//模拟自己打开自己window.close();//再关闭就没有对话框了</script>--------------------------以下是过多少秒自动关闭网页,弹窗<html><head><title>JS定时…

hibernate annotation注解方式来处理映射关系

2019独角兽企业重金招聘Python工程师标准>>> 在hibernate中&#xff0c;通常配置对象关系映射关系有两种&#xff0c;一种是基于xml的方式&#xff0c;另一种是基于annotation的注解方式&#xff0c;熟话说&#xff0c;萝卜青菜&#xff0c;可有所爱&#xff0c;每个…

操作系统Ubuntu(实验一二)

摘录&#xff1a;https://www.cnblogs.com/penglang14/p/10632360.html 实验一二1.1_小技巧1.2_ls查看目录命令1.3_cd切换目录命令1.4_查看文件内容命令(1)cat filename(2)more filename(3)head [-n] filename([]表示此内容可有可无)&#xff08;4&#xff09;wc命令确定行数、…

Redis 是如何执行的?

在以往的面试中&#xff0c;当问到一些面试者&#xff1a;Redis 是如何执行的&#xff1f;收到的答案往往是&#xff1a;客户端发命令给服务器端&#xff0c;服务端收到执行之后再返回给客户端。然而对于执行细节却「避而不谈」 &#xff0c;当继续追问服务器端是如何执行的&am…

java 枚举迭代_Java中的枚举和迭代器之间的区别

java 枚举迭代Java中的枚举与迭代器 (Enumeration vs Iterator in Java) Here, we will see how Enumeration differs from Iterator? 在这里&#xff0c;我们将看到Enumeration与Iterator有何不同&#xff1f; 1)枚举 (1) Enumeration) Enumeration is an interface which i…

ORA-14551: 无法在查询中执行 DML 操作

最近在调试一个带DML操作的函数时&#xff0c;一直不成功&#xff0c;在PL/SQL中测试时没问题&#xff0c;通过SQL语句调用函数时就不行了&#xff0c;刚开始一直没找到原因&#xff0c;后来无意间把 函数中捕获异常的代码注释掉&#xff0c;终于通过SQL调试时&#xff0c;弹出…

第五章I/O管理

I/O章节5.1.1I/O分类&#xff08;1&#xff09;按使用特性分&#xff08;2&#xff09;I/O设备按传输速率分类&#xff08;3&#xff09;I/O设备按信息交换的单位分5.1.2I/O控制器5.1.3I/O控制方式&#xff08;1&#xff09;程序直接控制方式&#xff08;轮询&#xff09;&…

阿里巴巴分布式服务框架 Dubbo

1.Dubbo是阿里巴巴内部的SOA服务化治理方案的核心框架&#xff0c;每天为2000 个服务提供3,000,000,000 次访问量支持&#xff0c;并被广泛应用于阿里巴巴集团的各成员站点。Dubbo自2011年开源后&#xff0c;已被许多非阿里系公司使用。 2.入门文档 http://alibaba.github.io/d…

列表使用与内部实现原理

列表类型 (List) 是一个使用链表结构存储的有序结构,它的元素插入会按照先后顺序存储到链表结构中,因此它的元素操作 (插入\删除) 时间复杂度为 O(1),所以相对来说速度还是比较快的,但它的查询时间复杂度为 O(n),因此查询可能会比较慢。 1 基础使用 列表类型的使用相对来…

c ++查找字符串_C ++类和对象| 查找输出程序| 套装1

c 查找字符串Program 1: 程序1&#xff1a; #include <iostream>using namespace std;class Sample {privateint A;privateint B;publicvoid init(){A 10;B 20;}publicvoid print(){cout << A << " " << B;}};int main(){Sample S;S.init…

Oracle 练习P297 131026 PL/SQL块程序

--1、编写一个PL/SQL块&#xff0c;输出所有员工的员工姓名&#xff0c;员工号、工资和部门号。beginfor v_emp in (select * from emp) loopdbms_output.put(员工姓名&#xff1a; || v_emp.ename);dbms_output.put(&#xff0c;员工号&#xff1a; || v_emp.empno);dbms_outp…

操作系统习题

操作系统习题习题一一、选择习题二一、选择二、综合题习题三一、选择题&#xff1f;二、简答题进程互斥遵循的四个原则&#xff1a;空闲让进、忙则等待、有限等待、让权等待重点习题四一、选择&#xff1f;&#xff1f;二、综合题死锁产生的 4 个必要条件是&#xff1a; &#…

WCF trace、log

1. 打开wcf配置&#xff1a; &#xff12;. enable trace &#xff0c; log 可以改变log路径&#xff1a; &#xff13;. 用 SvcTraceViewer.exe &#xff08;直接在c盘下搜索&#xff09; 查看 &#xff14;. 如果想自定义trace&#xff1a; catch(Exception ex) { Trace.Writ…

字典使用与内部实现原理

字典类型 (Hash) 又被成为散列类型或者是哈希表类型,它是将一个键值 (key) 和一个特殊的“哈希表”关联起来,这个“哈希表”表包含两列数据:字段和值。例如我们使用字典类型来存储一篇文章的详情信息,存储结构如下图所示: 同理我们也可以使用字典类型来存储用户信息,并且…

游标复习笔记

--while循环访问游标declarecursor cur_dept isselect * from dept;v_dept cur_dept%rowtype;beginopen cur_dept;fetch cur_dept into v_dept;while cur_dept%found loopdbms_output.put_line(v_dept.dname);fetch cur_dept into v_dept;end loop;close cur_dept;end;--retur…

操作系统中同步_操作系统中的经典同步问题

操作系统中同步经典同步问题 (Classical synchronization problem) In this section, we present a number of different philosopher synchronization problems that are important mainly because they are examples for a large class of concurrency- control problems. Th…

算法设计与分析复习第一二章(时间复杂度和蛮力法)

算法复习一二章第一章时间复杂度第二章蛮力法&#xff08;1&#xff09;查找问题顺序查找&#xff08;2&#xff09;排序问题选择排序起泡排序&#xff08;3&#xff09;组合问题0-1bag问题概述&#xff08;略&#xff09;&#xff08;4&#xff09;图问题哈密顿回路TSP问题&am…

有序集合使用与内部实现原理

有序集合类型 (Sorted Set) 相比于集合类型多了一个排序属性 score(分值),对于有序集合 ZSet 来说,每个存储元素相当于有两个值组成的,一个是有序结合的元素值,一个是排序值。有序集合的存储元素值也是不能重复的,但分值是可以重复的。 当我们把学生的成绩存储在有序集…

Ubuntu12环境下Thin+rails(4)+ruby(2)+nginx+mysql 配置

Ubuntu12环境下Thinrails(4)ruby(2)nginxmysql配置1&#xff0e; 前提条件&#xff1a;已经正确安装了ubuntu12并且更行了源。2&#xff0e; 安装过程&#xff1a;2.1 安装ruby前的准备&#xff1a;1.1修改 /etc/apt/sources.list文件改为mirrors.163.com保存退出…

Oracle 游标的练习

--1、什么是游标&#xff1f;使用游标的基本步骤是什么&#xff1f; /*挡在PL/SQL块中执行查询语句&#xff08;SELECT&#xff09;和数据操纵语句&#xff08;DML&#xff09;时&#xff0c;Oracle会在内存中分配一个缓冲区&#xff0c;缓冲区中包含了处理过程的必需信息&…