Python实现比较两个列表(list)范围


Posted in Python onJune 12, 2015

有一道题: 比较两个列表范围,如果包含的话,返回TRUE,否则FALSE。 详细题目如下:

Create a function, this function receives two lists as parameters, each list indicates a scope of numbers, the function judges whether list2 is included in list1.

 Function signature:
    differ_scope(list1, list2)

 Parameters:
    list1, list2    - list1 and list2 are constructed with strings,
                      each string indicates a number or a scope of
                      numbers. The number or scope are randomly, can
                      be overlapped. All numbers are positive.

                        E.g.
                            ['23', '44-67', '12', '3', '20-90']
 Return Values:
    True            - if all scopes and numbers indicated by list2 are included in list1.
    False           - if any scope or number in list2 is out of the range in list1.
 Examples:
    case1    - list1 = ['23', '44-67', '12', '3', '20-90']
               list2 = ['22-34', '33', 45', '60-61']
               differ_scope(list1, list2) == True
    case2    - list1 = ['23', '44-67', '12', '3', '20-90']
               list2 = ['22-34', '33', 45', '60-61', '100']
               differ_scope(list1, list2) == False

贴上自己写的代码如下:(备注: python 2.7.6)

def differ_scope(list1, list2): 
  print "list1:" + str(list1) 
  print "list2:" + str(list2) 
  #设置临时存放列表 
  list1_not_ = [] #用于存放列表1正常的数字值,当然要用int()来转换 
  list1_yes_ = [] #用于存放列表1中范围值如 44-67 
  list1_final = [] #用于存放列表1中最终范围值 如:[1,2,3,4,5,6,7,8,9,10] 
  temp1    = [] 
   
  list2_not_ = []  #用于存放列表2正常的数字值,当然要用int()来转换 
  list2_yes_ = []  #用于存放列表2中范围值如 44-67 
  list2_final= []  #用于存放列表2中最终范围值 如:[1,2,3,4,5,6,7,8,9,10] 
  temp2   = [] 
 
  temp    = []  #用于存放列表1,与列表2比较后的列表,从而判断结果为True还是False. 
   
  #对列表1进行处理 
  for i in range(len(list1)): #用FOR循环对列表1进行遍历 
    tag = 0 
    if list1[i].find('-')>0:#对含范围的数字进行处理,放到list_yes_列表中  
      strlist = list1[i].split('-') 
    list1_yes_ = range(int(strlist[0]),int(strlist[1])+1)#让其生成一个范围列表 
    for each in list1_yes_:     #FOR循环遍历所有符合条件的. 
        [temp1.append(each)] 
    else:           #对列表1中正常的数字进行处理,放到list_not_列表中 
      list1_not_.append(int(list1[i]))#对列表1中进行处理,放到list_yes_    
  [temp1.append(i) for i in list1_not_ if not i in temp1]#去除重复项 
  list1_final = sorted(temp1) #比较后,排序,并放到list1_final列表中 
  print "list1_final value is:" + str(list1_final)#打印排序后最终list1_final列表 
 
   
  #对列表2进行处理 
  for i in range(len(list2)): 
    if list2[i].find('-')>0: 
      strlist = list2[i].split('-') 
    list2_yes_ = range(int(strlist[0]),int(strlist[1])+1) 
    for each in list2_yes_: 
        [temp2.append(each)] 
      print "Temp2:" + str(temp2) 
    else: 
      list2_not_.append(int(list2[i])) 
  [temp2.append(i) for i in list2_not_ if not i in temp2] 
  list2_final = sorted(temp2) 
  print "list2_final value is:" + str(list2_final) 
 
  #对两个列表进行比较,得出最终比较结果. 
  [temp.append(i) for i in list2_final if not i in list1_final]#比较两个列表差值. 
  print "In list2 but not in list1:%s" % (temp)#打印出列表1与列表2的差值 
  if len(temp)>=1 : 
    print "The result is: False" 
  else: 
    print "The result is: True" 
 
if __name__ == '__main__': 
  list1 = ['23', '44-67', '12', '3','90-100'] 
  list2 = ['22-34', '33', '45'] 
  differ_scope(list1,list2)

总结:
1. 这道题关键是想法,如果整成坐标的方式来比较,会很麻烦。
2. 列表转成范围后,如果消除重复项,同样是里面的关键所在。
3. 其次是对列表遍历的操作,同样挺重要。

Python 相关文章推荐
Python与Redis的连接教程
Apr 22 Python
批处理与python代码混合编程的方法
May 19 Python
如何使用Python进行OCR识别图片中的文字
Apr 01 Python
将Python字符串生成PDF的实例代码详解
May 17 Python
pytorch:torch.mm()和torch.matmul()的使用
Dec 27 Python
使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例
Mar 16 Python
后端开发使用pycharm的技巧(推荐)
Mar 27 Python
Python run()函数和start()函数的比较和差别介绍
May 03 Python
python redis存入字典序列化存储教程
Jul 16 Python
Python读取Excel一列并计算所有对象出现次数的方法
Sep 04 Python
Python之京东商品秒杀的实现示例
Jan 06 Python
python实现ROA算子边缘检测算法
Apr 05 Python
在Linux系统上安装Python的Scrapy框架的教程
Jun 11 #Python
Python语言实现机器学习的K-近邻算法
Jun 11 #Python
在Linux下使用Python的matplotlib绘制数据图的教程
Jun 11 #Python
python中的代码编码格式转换问题
Jun 10 #Python
python实现数独算法实例
Jun 09 #Python
python中的全局变量用法分析
Jun 09 #Python
python简单实现计算过期时间的方法
Jun 09 #Python
You might like
PHPWind与Discuz截取字符函数substrs与cutstr性能比较
2011/12/05 PHP
PHP定时执行计划任务的多种方法小结
2011/12/19 PHP
php中一个有意思的日期逻辑处理
2012/03/25 PHP
php输入流php://input使用示例(php发送图片流到服务器)
2013/12/25 PHP
ThinkPHP使用心得分享-分页类Page的用法
2014/05/15 PHP
PHP函数实现分页含文本分页和数字分页
2014/10/23 PHP
Zend Framework教程之路由功能Zend_Controller_Router详解
2016/03/07 PHP
PHP微信公众号自动发送红包API
2016/06/01 PHP
php正则提取html图片(img)src地址与任意属性的方法
2017/02/08 PHP
php实现的数组转xml案例分析
2019/09/28 PHP
jquery动画4.升级版遮罩效果的图片走廊--带自动运行效果
2012/08/24 Javascript
各种页面定时跳转(倒计时跳转)代码总结
2013/10/24 Javascript
js给onclick赋值传参数的两种方法
2013/11/25 Javascript
JS的encodeURI和java的URLDecoder.decode使用介绍
2014/05/08 Javascript
基于javascript实现浏览器滚动条快到底部时自动加载数据
2015/11/30 Javascript
noty ? jQuery通知插件全面解析
2016/05/18 Javascript
jquery常用的12个小功能
2016/07/22 Javascript
Javascript点击按钮随机改变数字与其颜色
2016/09/01 Javascript
jQuery实现智能判断固定导航条或侧边栏的方法
2016/09/04 Javascript
JavaScript基于对象去除数组重复项的方法
2016/10/09 Javascript
[Bootstrap-插件使用]Jcrop+fileinput组合实现头像上传功能实例代码
2016/12/20 Javascript
javascript笔记之匿名函数和闭包
2017/02/06 Javascript
Bootstrap table简单使用总结
2017/02/15 Javascript
vue键盘事件点击事件加native操作
2020/07/27 Javascript
python技能之数据导出excel的实例代码
2017/08/11 Python
python3实现基于用户的协同过滤
2018/05/31 Python
浅谈python 中类属性共享的问题
2019/07/02 Python
Python3.x+迅雷x 自动下载高分电影的实现方法
2020/01/12 Python
Python Flask框架实现简单加法工具过程解析
2020/06/03 Python
优秀交警事迹材料
2014/01/26 职场文书
本科毕业论文指导教师评语
2014/12/30 职场文书
党员年度个人总结
2015/02/14 职场文书
缅怀先烈主题班会
2015/08/14 职场文书
go 实现简易端口扫描的示例
2021/05/22 Golang
总结Pyinstaller打包的高级用法
2021/06/28 Python
Java多线程并发FutureTask使用详解
2022/06/28 Java/Android