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中用于返回绝对值的abs()方法
May 14 Python
Python过滤列表用法实例分析
Apr 29 Python
20招让你的Python飞起来!
Sep 27 Python
Python机器学习之决策树算法实例详解
Dec 06 Python
在cmd命令行里进入和退出Python程序的方法
May 12 Python
详解Django-restframework 之频率源码分析
Feb 27 Python
python使用装饰器作日志处理的方法
Jul 11 Python
Python generator生成器和yield表达式详解
Aug 08 Python
详解Python中的正斜杠与反斜杠
Aug 09 Python
python实现网站微信登录的示例代码
Sep 18 Python
python交互模式基础知识点学习
Jun 18 Python
jupyter notebook远程访问不了的问题解决方法
Jan 11 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
phpadmin如何导入导出大数据文件及php.ini参数修改
2013/02/18 PHP
Windows下php+mysql5.7配置教程
2017/05/16 PHP
PHP的JSON封装、转变及输出操作示例
2019/09/27 PHP
jQuery 阴影插件代码分享
2012/01/09 Javascript
利用JQuery和JS实现奇偶行背景颜色自定义效果
2012/11/19 Javascript
JavaScript onkeypress事件入门实例(按下或按住一个键盘按键)
2014/10/17 Javascript
javascript面向对象之定义成员方法实例分析
2015/01/13 Javascript
Angularjs中$http以post请求通过消息体传递参数的实现方法
2016/08/05 Javascript
JS日期对象简单操作(获取当前年份、星期、时间)
2016/10/26 Javascript
JS中如何实现Laravel的route函数详解
2017/02/12 Javascript
jQuery remove()过滤被删除的元素(推荐)
2017/07/18 jQuery
vue中如何去掉空格的方法实现
2018/11/09 Javascript
构建Vue大型应用的10个最佳实践(小结)
2019/11/07 Javascript
ng-alain的sf如何自定义部件的流程
2020/06/12 Javascript
Python使用struct处理二进制的实例详解
2017/09/11 Python
Python数据结构与算法之图的基本实现及迭代器实例详解
2017/12/12 Python
ipython和python区别详解
2019/06/26 Python
Django1.11配合uni-app发起微信支付的实现
2019/10/12 Python
PyTorch实现ResNet50、ResNet101和ResNet152示例
2020/01/14 Python
Python发送手机动态验证码代码实例
2020/02/28 Python
Selenium元素定位的30种方式(史上最全)
2020/05/11 Python
Python Pandas list列表数据列拆分成多行的方法实现
2020/12/14 Python
基于CSS3的CSS 多栏(Multi-column)实现瀑布流源码分享
2014/06/11 HTML / CSS
印度在线内衣和时尚目的地:Zivame
2017/09/28 全球购物
Farfetch美国:奢侈品牌时尚购物平台
2019/05/02 全球购物
英国运动风奢侈品购物网站:Maison De Fashion
2020/08/28 全球购物
RIP版本1跟版本2的区别
2013/12/30 面试题
校园十大歌手策划书
2014/02/01 职场文书
电子商务助理求职自荐信
2014/04/10 职场文书
机械工程学院大学生求职信
2014/05/25 职场文书
2014年社区党建工作汇报材料
2014/11/02 职场文书
教师考核表个人总结
2015/02/12 职场文书
人事行政部各岗位职责说明书!
2019/07/15 职场文书
html5实现点击弹出图片功能
2021/07/16 HTML / CSS
nginx搭建NFS网络文件系统
2022/04/14 Servers
python中mongodb包操作数据库
2022/04/19 Python