python --判断列表重复
一、判断单个列表中的元素是否存在重复
使用set方法去重后,和原list进行对比,如果相等,那么说明原列表无重复,如果存在重复,说明列表存在重复
def is_repect_all(L):
repeatList = [];
setList = set(L);
flag=True;
if len(L) != len(setList):
flag=False;
print('列表的id存在重复,其中重复项及重复次数如下:');
for each_item in setList:
re_count = 0;
for each_item_L in L:
if each_item == each_item_L:
re_count += 1;
if (re_count >= 2):
print(each_item, L.index(each_item), ": ", re_count); # print(all_gameid_List[L.index(each_item)]);
return flag,re_count; else:
return flag;
二、判断两个列表是否重复:
#判断两个列表是否含有重复项
#方法有多种,此处选择最简单的方法,使用set()
def is_repeat_alls(L1,L2):
if(len(L1)==0 or len(L2)==0):
return '无法比对,原因:其中一个list为空';
else: #L1单列表去重输出
set_L1=set(L1);
#L2单列表去重输出
set_L2=set(L2);
#输出L1、L2合集
set_L3=set_L1&set_L2;
#将合集去重,并转换成list,注意,set不是迭代器
list_L3=list(set_L3);
if(len(list_L3)==0):
#如果列表为空,则表示没有重复项,则测试通过
print('无重复项,通过');
else:
#如果列表不为空,则表示存在重复项,则表示测试失败,此时打印出列表即可
print('测试不通过,存在重复项');
return '共'+str(len(list_L3))+'个重复,分别为:',list_L3;
三、判断多个列表是否两两重复
python --判断列表重复的更多相关文章
- Python判断列表是否已排序的各种方法及其性能分析
目录 Python判断列表是否已排序的各种方法及其性能分析 声明 一. 问题提出 二. 代码实现 2.1 guess 2.2 sorted 2.3 for-loop 2.4 all 2.5 numpy ...
- python 去除列表重复元素方法汇总
1.使用set集合,虽然去除掉重复元素,但是顺序改变了 耗时约4.0*10^-5 s A = ['a','b','X','a','b','G'] B = list(set(A)) print(A)[' ...
- python 判断列表字符串元素首尾字符是否相同
def match_words(words): ctr = for word in words: and word[] == word[-]: ctr += return ctr print(matc ...
- python 判断列表的包含关系
def is_Sublist(l, s): sub_set = False if s == []: sub_set = True elif s == l: sub_set = True elif le ...
- python删除列表重复元素
用list类的sort方法 l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) l2.sort(key=l1.index) print l2
- Python遍历列表删除多个元素或者重复元素
在遍历list的时候,删除符合条件的数据,结果不符合预期 num_list = [1, 2, 2, 2, 3] print(num_list) for item in num_list: if i ...
- Python统计列表中的重复项出现的次数的方法
本文实例展示了Python统计列表中的重复项出现的次数的方法,是一个很实用的功能,适合Python初学者学习借鉴.具体方法如下:对一个列表,比如[1,2,2,2,2,3,3,3,4,4,4,4],现在 ...
- Python 去除列表中重复的元素
Python 去除列表中重复的元素 来自比较容易记忆的是用内置的set l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) print l2 还 ...
- python去除列表中重复元素的方法
列表中元素位置的索引用的是L.index 本文实例讲述了Python去除列表中重复元素的方法.分享给大家供大家参考.具体如下: 比较容易记忆的是用内置的set 1 2 3 l1 = ['b','c', ...
随机推荐
- WebApi中使用session
webapi默认是不支持session的,要通过一些手动配置来开启Session功能 在Global.asax里添加: 导入命名空间: using System.Web.SessionState; p ...
- importlib模块
importlib模块 import importlib根据这个字符串来导入这个模块的 a=importlib.import_module('xx.oo')print(a.Person())里面可以传 ...
- SpriteKit 学习体会贴(不断完善中)
1. 关于 SKShapeNode 刚接触SpriteKit时,看到这个类,以为它会比SKSpriteNode更为轻量级,但其实不是: Shape nodes are useful for conte ...
- centos6.5 Python.7 pip install PIL --allow-external PIL --allow-unverified PIL报错 no such option: --allow-external
解决办法 pip install pillow 使用from PIL import Image ,正常!!
- vue 轮播图插件 vue-awesome-swiper
1.npm安装 npm install vue-awesome-swiper --save 2.vue 引入 //在main.js 中全局引入 import VueAwesomeSwiper from ...
- SecureCRT中sqlplus,使用Backspace删除时 ^H^H
平时习惯用Backspace删除输入错误,但是在SecureCRT中使用是,却是: SQL> sele^H^H 网上有几个方法,觉得改SecureCRT的配置最方便.
- Confluence 6 在数据源连接中启用校验查询
确定 Confluence 在数据库连接池中校验数据库连接: 停止 Confluence. 编辑 <installation-directory>/conf/server.xml 文件(或 ...
- php模拟数据请求
php:模拟后台接受数据的步骤<?php> 1.连接数据库 $host="localhost"; $uname="root"; $upwd=&quo ...
- 异常:Keyword not supported: 'data source'的解决办法
将连接字符串中的"换为“'”,一个单引号即可. 详细解释:https://blogs.msdn.microsoft.com/rickandy/2008/12/09/explicit-c ...
- Reading Refs
有时候看论文时会有一种发现“新大陆”的感觉......也许这就是科研魅力之一!