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使用reportlab实现图片转换成pdf的方法
May 22 Python
python实现将内容分行输出
Nov 05 Python
在CentOS6上安装Python2.7的解决方法
Jan 09 Python
python list元素为tuple时的排序方法
Apr 18 Python
python 删除非空文件夹的实例
Apr 26 Python
Python SVM(支持向量机)实现方法完整示例
Jun 19 Python
Python文件常见操作实例分析【读写、遍历】
Dec 10 Python
python3实现表白神器
Apr 09 Python
python实现的汉诺塔算法示例
Oct 23 Python
Python定义函数时参数有默认值问题解决
Dec 19 Python
Python实现随机生成任意数量车牌号
Jan 21 Python
python中添加模块导入路径的方法
Feb 03 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 self,$this,const,static,->的使用
2009/10/22 PHP
PHP 万年历实现代码
2012/10/18 PHP
PHP编译安装中遇到的两个错误和解决方法
2014/08/20 PHP
php数组排序usort、uksort与sort函数用法
2014/11/17 PHP
PHP实现新型冠状病毒疫情实时图的实例
2020/02/04 PHP
jquery ajax 局部刷新小案例
2014/02/08 Javascript
js文件Cookie存取值示例代码
2014/02/20 Javascript
js实现图片拖动改变顺序附图
2014/05/13 Javascript
js中运算符&& 和 || 的使用记录
2014/08/21 Javascript
优化Node.js Web应用运行速度的10个技巧
2014/09/03 Javascript
JavaScript实现自动对页面上敏感词进行屏蔽的方法
2015/07/27 Javascript
js鼠标单击和双击事件冲突问题的快速解决方法
2016/07/11 Javascript
Bootstrap零基础学习第一课之模板
2016/07/18 Javascript
如何在JS中实现相互转换XML和JSON
2016/07/19 Javascript
vue实现ToDoList简单实例
2017/02/07 Javascript
webpack自动打包和热更新的实现方法
2019/06/24 Javascript
vue 使用高德地图vue-amap组件过程解析
2019/09/07 Javascript
layer.prompt使文本框为空的情况下也能点击确定的方法
2019/09/24 Javascript
vue transition 在子组件中失效的解决
2019/11/12 Javascript
[42:24]完美世界DOTA2联赛PWL S2 LBZS vs FTD.C 第三场 11.27
2020/12/01 DOTA
python文件读写并使用mysql批量插入示例分享(python操作mysql)
2014/02/17 Python
详解python脚本自动生成需要文件实例代码
2017/02/04 Python
python实现图书管理系统
2018/03/12 Python
如何使用 Pylint 来规范 Python 代码风格(来自IBM)
2018/04/06 Python
详解Python中pandas的安装操作说明(傻瓜版)
2019/04/08 Python
树莓派动作捕捉抓拍存储图像脚本
2019/06/22 Python
详解PyTorch手写数字识别(MNIST数据集)
2019/08/16 Python
python 生成任意形状的凸包图代码
2020/04/16 Python
CSS实现雨滴动画效果的实例代码
2019/10/08 HTML / CSS
英国儿童家具专卖店:GLTC
2016/09/24 全球购物
广州地球村科技数据库题目
2016/04/25 面试题
毕业生求职信的经典写法
2014/01/31 职场文书
《商鞅南门立木》教学反思
2014/02/16 职场文书
幼儿园托班开学寄语(2016秋季)
2015/12/03 职场文书
致毕业季:你如何做好自己的职业生涯规划书?
2019/07/01 职场文书
Windows Server 2012 R2服务器安装与配置的完整步骤
2022/07/15 Servers