Python3查找列表中重复元素的个数的3种方法详解

作者:猪笨是念来过倒 时间:2022-08-01 16:36:07 

方法一:


mylist = [1,2,2,2,2,3,3,3,4,4,4,4]
myset = set(mylist)
for item in myset:
 print("the %d has found %d" %(item,mylist.count(item)))

the 1 has found 1

the 2 has found 4

the 3 has found 3

the 4 has found 4

方法二:


from collections import Counter
Counter([1,2,2,2,2,3,3,3,4,4,4,4])
Counter({2: 4, 4: 4, 3: 3, 1: 1})

方法三:


List=[1,2,2,2,2,3,3,3,4,4,4,4]
a = {}
for i in List:
 if List.count(i)>1:
   a[i] = List.count(i)

来源:https://blog.csdn.net/liao392781/article/details/87257097

标签:Python,列表,重复
0
投稿

猜你喜欢

  • Python编写可视化界面的全过程(Python+PyCharm+PyQt)

    2022-02-23 02:21:36
  • 对Python3中dict.keys()转换成list类型的方法详解

    2021-02-13 12:18:26
  • PHP依赖注入原理与用法分析

    2023-09-04 01:22:54
  • python如何利用traceback获取详细的异常信息

    2023-06-14 00:53:18
  • Python标准库之随机数 (math包、random包)介绍

    2021-09-26 11:57:08
  • 关于设计规范

    2008-06-02 13:10:00
  • 使用python切片实现二维数组复制示例

    2021-11-02 22:24:41
  • 用CSS3将你的设计带入下个高度[译]

    2009-06-22 13:03:00
  • Sql server 2005 找出子表树

    2008-11-24 15:23:00
  • 利用Python实现最小二乘法与梯度下降算法

    2021-07-19 03:59:49
  • 网页常用特效整理:中级篇

    2013-07-15 13:43:32
  • Python下载网络小说实例代码

    2023-08-01 18:22:17
  • Python实现动态二维码生成的示例代码

    2022-08-03 15:45:39
  • 谈谈FACEBOOK的一处产品细节

    2008-03-11 11:05:00
  • python使用正则筛选信用卡

    2021-03-10 14:06:47
  • python 使用ctypes调用C/C++ dll详情

    2023-03-30 06:00:57
  • Python实现通讯录功能

    2022-06-28 22:33:26
  • php环境下利用session防止页面重复刷新的具体实现

    2023-11-14 08:51:45
  • goland中使用leetcode插件实现

    2023-07-23 10:43:09
  • jQuery 1.3.3 新功能[译]

    2009-06-04 12:23:00
  • asp之家 网络编程 m.aspxhome.com