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查看多台服务器进程的脚本分享
Jun 11 Python
Python pickle类库介绍(对象序列化和反序列化)
Nov 21 Python
pygame学习笔记(4):声音控制
Apr 15 Python
Python 通过URL打开图片实例详解
Jun 01 Python
python 输出上个月的月末日期实例
Apr 11 Python
关于Pycharm无法debug问题的总结
Jan 19 Python
Django+Xadmin构建项目的方法步骤
Mar 06 Python
Python实现堡垒机模式下远程命令执行操作示例
May 09 Python
SELENIUM自动化模拟键盘快捷键操作实现解析
Oct 28 Python
pytorch程序异常后删除占用的显存操作
Jan 13 Python
Python自动化之UnitTest框架实战记录
Sep 08 Python
完美解决Pycharm中matplotlib画图中文乱码问题
Jan 11 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
PHP开发的一些注意点总结
2010/10/12 PHP
PHP简洁函数(PHP简单明了函数语法)
2012/06/10 PHP
destoon实现首页显示供应、企业、资讯条数的方法
2014/07/15 PHP
PHP批量去除BOM头代码分享
2015/06/26 PHP
Laravel5.1 框架Request请求操作常见用法实例分析
2020/01/04 PHP
JavaScript中的事件处理
2008/01/16 Javascript
javascript针对DOM的应用分析(三)
2012/04/15 Javascript
JQuery查找DOM节点的方法
2015/06/11 Javascript
Js调用Java方法并互相传参的简单实例
2016/08/11 Javascript
Bootstrap幻灯片轮播图支持触屏左右手势滑动的实现方法
2016/10/13 Javascript
React-router 4 按需加载的实现方式及原理详解
2017/05/25 Javascript
详解vue项目首页加载速度优化
2017/10/18 Javascript
vue单页面打包文件大?首次加载慢?nginx带你飞,从7.5M到1.3M蜕变过程(推荐)
2018/01/16 Javascript
vue2.0 循环遍历加载不同图片的方法
2018/03/06 Javascript
Vue数字输入框组件示例代码详解
2020/01/15 Javascript
vue-resourc发起异步请求的方法
2020/02/11 Javascript
详解Python中类的定义与使用
2017/04/11 Python
Python编程实现从字典中提取子集的方法分析
2018/02/09 Python
python去除扩展名的实例讲解
2018/04/23 Python
Windows下PyCharm安装图文教程
2018/08/27 Python
Python中整数的缓存机制讲解
2019/02/16 Python
Python基于paramunittest模块实现excl参数化
2020/04/26 Python
tensorflow实现残差网络方式(mnist数据集)
2020/05/26 Python
python 读取、写入txt文件的示例
2020/09/27 Python
如何用用Python将地址标记在地图上
2021/02/07 Python
HTML5几个设计和修改的页面范例分享
2015/09/29 HTML / CSS
使用phonegap查找联系人的实现方法
2017/03/31 HTML / CSS
船餐厅和泰晤士河餐饮游轮:Bateaux London
2018/03/19 全球购物
国际商务英语专业求职信
2014/07/08 职场文书
学校感恩节活动策划方案
2014/10/06 职场文书
春风化雨观后感
2015/06/11 职场文书
听证会主持词
2015/07/03 职场文书
中秋节感想
2015/08/10 职场文书
禁毒心得体会范文
2016/01/15 职场文书
解决thinkphp6(tp6)在状态码500下不报错,或者显示错误“Malformed UTF-8 characters”的问题
2021/04/01 PHP
Spring Boot配合PageHelper优化大表查询数据分页
2022/04/20 Java/Android