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 相关文章推荐
pycharm 使用心得(九)解决No Python interpreter selected的问题
Jun 06 Python
用实例详解Python中的Django框架中prefetch_related()函数对数据库查询的优化
Apr 01 Python
python下调用pytesseract识别某网站验证码的实现方法
Jun 06 Python
python django 实现验证码的功能实例代码
May 18 Python
Python数据操作方法封装类实例
Jun 23 Python
Windows下PyCharm2018.3.2 安装教程(图文详解)
Oct 24 Python
pytorch 修改预训练model实例
Jan 18 Python
postman和python mock测试过程图解
Feb 22 Python
keras 使用Lambda 快速新建层 添加多个参数操作
Jun 10 Python
Python如何在windows环境安装pip及rarfile
Jun 15 Python
Python Opencv轮廓常用操作代码实例解析
Sep 01 Python
matplotlib grid()设置网格线外观的实现
Feb 22 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的正则处理函数总结分析
2008/06/20 PHP
PHP 面向对象详解
2012/09/13 PHP
php通过rmdir删除目录的简单用法
2015/03/18 PHP
php实现图片转换成ASCII码的方法
2015/04/03 PHP
浅析php如何实现App常用的秒发功能
2016/08/03 PHP
javascript for循环从入门到偏门(效率优化+奇特用法)
2012/08/01 Javascript
关于jQuery新的事件绑定机制on()的使用技巧
2013/04/26 Javascript
JavaScript 实现类的多种方法实例
2013/05/01 Javascript
jQuery实现产品对比功能附源码下载
2016/08/09 Javascript
jQuery dataTables与jQuery UI 对话框dialog的使用教程
2016/09/02 Javascript
使用AngularJS2中的指令实现按钮的切换效果
2017/03/27 Javascript
vue组件 $children,$refs,$parent的使用详解
2017/07/31 Javascript
详解Node中导入模块require和import的区别
2017/08/11 Javascript
vue中axios的二次封装实例讲解
2019/10/14 Javascript
JavaScript 浏览器对象模型BOM原理与常见用法实例分析
2019/12/16 Javascript
vuex存取值和映射函数使用说明
2020/07/24 Javascript
[02:30]DOTA2放量测试专访海涛:呼吁保护新手玩家
2013/08/26 DOTA
Python函数中*args和**kwargs来传递变长参数的用法
2016/01/26 Python
python简单实现刷新智联简历
2016/03/30 Python
浅谈python中字典append 到list 后值的改变问题
2018/05/04 Python
Django 根据数据模型models创建数据表的实例
2018/05/27 Python
pandas的唯一值、值计数以及成员资格的示例
2018/07/25 Python
详解Python3 对象组合zip()和回退方式*zip
2019/05/15 Python
Python分割训练集和测试集的方法示例
2019/09/19 Python
python通过链接抓取网站详解
2019/11/20 Python
Django使用list对单个或者多个字段求values值实例
2020/03/31 Python
python中Mako库实例用法
2020/12/31 Python
Sneaker Studio捷克:购买运动鞋
2018/07/08 全球购物
您的时尚,您的生活方式:DTLR Villa
2019/12/25 全球购物
青蓝工程实施方案
2014/03/27 职场文书
社区活动总结报告
2014/05/05 职场文书
超市开业庆典策划方案
2014/05/14 职场文书
债务追讨律师函
2015/06/24 职场文书
大一新生军训新闻稿
2015/07/17 职场文书
2019年最新感恩节祝福语(28句)
2019/11/27 职场文书
pycharm代码删除恢复的方法
2021/06/26 Python