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 相关文章推荐
Scrapy抓取京东商品、豆瓣电影及代码分享
Nov 23 Python
Python实现调度算法代码详解
Dec 01 Python
Python PyQt4实现QQ抽屉效果
Apr 20 Python
python 判断参数为Nonetype类型或空的实例
Oct 30 Python
Python遍历文件夹 处理json文件的方法
Jan 22 Python
Pyqt QImage 与 np array 转换方法
Jun 27 Python
Django工程的分层结构详解
Jul 18 Python
django做form表单的数据验证过程详解
Jul 26 Python
Python递归函数特点及原理解析
Mar 04 Python
PyCharm 2020 激活到 2100 年的教程
Mar 25 Python
PIL.Image.open和cv2.imread的比较与相互转换的方法
Jun 03 Python
python实现的web监控系统
Apr 27 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中使用redis
2013/11/04 PHP
php实现在线考试系统【附源码】
2018/09/18 PHP
XAMPP升级PHP版本实现步骤解析
2020/09/04 PHP
动态添加js事件实现代码
2009/03/12 Javascript
通过Mootools 1.2来操纵HTML DOM元素
2009/09/15 Javascript
javascript的onchange事件与jQuery的change()方法比较
2009/09/28 Javascript
jquery下json数组的操作实现代码
2010/08/09 Javascript
IE图片缓存document.execCommand("BackgroundImageCache",false,true)
2011/03/01 Javascript
JavaScript中数组的排序、乱序和搜索实现代码
2011/11/30 Javascript
JS模板实现方法
2013/04/03 Javascript
简单实用jquery版三级联动select示例
2013/07/04 Javascript
20行代码实现的一个CSS覆盖率测试脚本
2013/07/07 Javascript
js实现交换运动效果的方法
2015/04/10 Javascript
Jquery简单实现GridView行高亮的方法
2015/06/15 Javascript
浅谈jQuery的offset()方法及示例分享
2015/07/17 Javascript
js显示动态时间的方法详解
2016/08/20 Javascript
关于javascript原型的修改与重写(覆盖)差别详解
2016/08/31 Javascript
javascript淘宝主图放大镜功能
2016/10/20 Javascript
NodeJs的fs读写删除移动监听
2017/04/28 NodeJs
laravel-admin 与 vue 结合使用实例代码详解
2019/06/04 Javascript
python扫描proxy并获取可用代理ip的实例
2017/08/07 Python
Python实现求数列和的方法示例
2018/01/12 Python
Zookeeper接口kazoo实例解析
2018/01/22 Python
Python 确定多项式拟合/回归的阶数实例
2018/12/29 Python
远程部署工具Fabric详解(支持Python3)
2019/07/04 Python
python框架flask表单实现详解
2019/11/04 Python
Python统计时间内的并发数代码实例
2019/12/28 Python
深入了解NumPy 高级索引
2020/07/24 Python
西班牙伏林航空公司:Vueling
2016/08/05 全球购物
老海军美国官网:Old Navy
2016/09/05 全球购物
缓解脚、腿和背部疼痛:Z-CoiL鞋
2019/03/12 全球购物
Levi’s西班牙官方网站:李维斯,著名的牛仔裤品牌
2020/08/20 全球购物
美德好少年事迹材料
2014/01/19 职场文书
2015年班级工作总结范文
2015/04/03 职场文书
2015暑假实习报告范文
2015/07/13 职场文书
幼师自荐信范文(2016推荐篇)
2016/01/28 职场文书