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通过自定义isnumber函数判断字符串是否为数字的方法
Apr 23 Python
在Python中操作时间之mktime()方法的使用教程
May 22 Python
Python中的Descriptor描述符学习教程
Jun 02 Python
Python编程求质数实例代码
Jan 31 Python
如何使用 Pylint 来规范 Python 代码风格(来自IBM)
Apr 06 Python
解决Mac下首次安装pycharm无project interpreter的问题
Oct 29 Python
Python设计模式之迭代器模式原理与用法实例分析
Jan 10 Python
Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】
Dec 19 Python
python使用Geany编辑器配置方法
Feb 21 Python
Python 保存加载mat格式文件的示例代码
Aug 04 Python
python类共享变量操作
Sep 03 Python
Python数据可视化常用4大绘图库原理详解
Oct 23 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
40年前的这部特摄片恐龙特级克塞号80后的共同回忆
2020/03/08 日漫
PHP初学者头疼问题总结
2006/10/09 PHP
PHP中exec函数和shell_exec函数的区别
2014/08/20 PHP
Yii1.1框架实现PHP极光推送消息通知功能
2018/09/06 PHP
javascript 在网页中的运用(asp.net)
2009/11/23 Javascript
jQuery前台数据获取实现代码
2011/03/16 Javascript
javascript中call和apply方法浅谈
2013/09/27 Javascript
JS组件Bootstrap按钮组与下拉按钮详解
2016/05/10 Javascript
javascript url几种编码方式详解
2016/06/06 Javascript
javascript将中国数字格式转换成欧式数字格式的简单实例
2016/08/02 Javascript
Angular工具方法学习
2016/12/26 Javascript
使用smartupload组件实现jsp+jdbc上传下载文件实例解析
2017/01/05 Javascript
Angular X中使用ngrx的方法详解(附源码)
2017/07/10 Javascript
vue mixins组件复用的几种方式(小结)
2017/09/06 Javascript
vue组件生命周期详解
2017/11/07 Javascript
JS运动特效之同时运动实现方法分析
2018/01/24 Javascript
vue二级菜单导航点击选中事件的方法
2018/09/12 Javascript
vue 实现cli3.0中使用proxy进行代理转发
2019/10/30 Javascript
Python3爬虫学习之将爬取的信息保存到本地的方法详解
2018/12/12 Python
Python PyQt5运行程序把输出信息展示到GUI图形界面上
2020/04/27 Python
Python更换pip源方法过程解析
2020/05/19 Python
PyQt中使用QtSql连接MySql数据库的方法
2020/07/28 Python
15个Pythonic的代码示例(值得收藏)
2020/10/29 Python
Python模拟登录requests.Session应用详解
2020/11/17 Python
canvas学习笔记之2d画布基础的实现
2019/02/21 HTML / CSS
英国护肤品购物网站:Beauty Expert
2016/08/19 全球购物
FitFlop澳大利亚官网:英国符合人体工学的鞋类品牌
2017/06/05 全球购物
Hotels.com越南:酒店预订
2019/10/29 全球购物
2014年教师节座谈会发言稿
2014/09/10 职场文书
知识就是力量演讲稿
2014/09/13 职场文书
企业授权委托书范本
2014/09/22 职场文书
酒店销售经理岗位职责
2015/04/02 职场文书
2015年国庆放假通知范文
2015/08/18 职场文书
如何在pycharm中快捷安装pip命令(如pygame)
2021/05/31 Python
Pygame Event事件模块的详细示例
2021/11/17 Python
vue/cli 配置动态代理无需重启服务的方法
2022/05/20 Vue.js