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中使用glob和rmtree删除目录子目录及所有文件的例子
Nov 21 Python
Python实现登录接口的示例代码
Jul 21 Python
Python+Socket实现基于TCP协议的客户与服务端中文自动回复聊天功能示例
Aug 31 Python
Java及python正则表达式详解
Dec 27 Python
python3 遍历删除特定后缀名文件的方法
Apr 23 Python
详解关于Django中ORM数据库迁移的配置
Oct 08 Python
Python的iOS自动化打包实例代码
Nov 22 Python
python3正则提取字符串里的中文实例
Jan 31 Python
Python调用C语言程序方法解析
Jul 07 Python
Python 执行矩阵与线性代数运算
Aug 01 Python
搭建pypi私有仓库实现过程详解
Nov 25 Python
如何在 Matplotlib 中更改绘图背景的实现
Nov 26 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
Terran兵种介绍
2020/03/14 星际争霸
PHP中SimpleXML函数用法分析
2014/11/26 PHP
PHP生成随机密码方法汇总
2015/08/27 PHP
php验证码实现代码(3种)
2015/09/07 PHP
windows下的WAMP环境搭建图文教程(推荐)
2017/07/27 PHP
用Javascript读取中文COOKIE的解决办法
2007/02/15 Javascript
javascript中获取选中对象的类型
2007/04/02 Javascript
JS上传图片前的限制包括(jpg jpg gif及大小高宽)等
2012/12/19 Javascript
jQuery阻止事件冒泡具体实现
2013/10/11 Javascript
js获取url中的参数且参数为中文时通过js解码
2014/03/19 Javascript
关闭页面时window.location事件未执行的原因分析及解决方案
2014/09/01 Javascript
JS基于VML技术实现的五角星礼花效果代码
2015/10/26 Javascript
基于JS实现仿京东搜索栏随滑动透明度渐变效果
2017/07/10 Javascript
详解vue-router 命名路由和命名视图
2018/06/01 Javascript
使用pkg打包Node.js应用的方法步骤
2018/10/19 Javascript
详解Webpack如何引入CDN链接来优化编译后的体积
2019/06/21 Javascript
vue中touch和click共存的解决方式
2020/07/28 Javascript
Flask SQLAlchemy一对一,一对多的使用方法实践
2013/02/10 Python
python通过字典dict判断指定键值是否存在的方法
2015/03/21 Python
用Python下载一个网页保存为本地的HTML文件实例
2018/05/21 Python
selenium+python自动化测试之页面元素定位
2019/01/23 Python
python实现指定ip端口扫描方式
2019/12/17 Python
python3中确保枚举值代码分析
2020/12/02 Python
详解如何使用Pytest进行自动化测试
2021/01/14 Python
法国综合购物网站:RueDuCommerce
2016/09/12 全球购物
DNA基因检测和分析:23andMe
2019/05/01 全球购物
英国最大的独立摄影零售商:Park Cameras
2019/11/27 全球购物
Linux面试经常问的文件系统操作命令
2015/11/05 面试题
酒店应聘自荐信
2013/11/09 职场文书
客服部班长工作责任制
2014/02/25 职场文书
庆国庆活动总结
2014/08/28 职场文书
假期安全教育广播稿
2014/10/04 职场文书
学生违反校规检讨书
2014/10/28 职场文书
python 使用tkinter与messagebox写界面和弹窗
2022/03/20 Python
vue 数字翻牌器动态加载数据
2022/04/20 Vue.js
MySQL count(*)统计总数问题汇总
2022/09/23 MySQL