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 RuntimeError: thread.__init__() not called解决方法
Apr 28 Python
Python编程实现二分法和牛顿迭代法求平方根代码
Dec 04 Python
Python 查找字符在字符串中的位置实例
May 02 Python
TensorFlow利用saver保存和提取参数的实例
Jul 26 Python
python使用__slots__让你的代码更加节省内存
Sep 05 Python
Python从使用线程到使用async/await的深入讲解
Sep 16 Python
pthon贪吃蛇游戏详细代码
Jan 27 Python
Django发送邮件和itsdangerous模块的配合使用解析
Aug 10 Python
python实现高斯判别分析算法的例子
Dec 09 Python
python实现根据给定坐标点生成多边形mask的例子
Feb 18 Python
解决keras backend 越跑越慢问题
Jun 18 Python
python如何进入交互模式
Jul 06 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使用gzip压缩传输js和css文件的方法
2015/07/29 PHP
zend框架实现支持sql server的操作方法
2016/12/08 PHP
PHP简单实现解析xml为数组的方法
2018/05/02 PHP
php设计模式之中介者模式分析【星际争霸游戏案例】
2020/03/23 PHP
JavaScript 动态将数字金额转化为中文大写金额
2009/05/14 Javascript
Javascript 实用小技巧
2010/04/07 Javascript
php跨域调用json的例子
2013/11/13 Javascript
javascript 获取网页标题代码实例
2014/01/22 Javascript
用js格式化金额可设置保留的小数位数
2014/05/09 Javascript
Jquery中map函数的用法
2016/06/03 Javascript
AngularJS的ng Http Request与response格式转换方法
2016/11/07 Javascript
BootStrap Validator对于隐藏域验证和程序赋值即时验证的问题浅析
2016/12/01 Javascript
利用jQuery.Validate异步验证用户名是否存在(推荐)
2016/12/09 Javascript
详解js数组的完全随机排列算法
2016/12/16 Javascript
three.js实现3D影院的原理的代码分析
2017/12/18 Javascript
JavaScript 五大常见函数
2018/03/23 Javascript
解决vue移动端适配问题
2018/12/12 Javascript
vue基础之data存储数据及v-for循环用法示例
2019/03/08 Javascript
Vue.use()在new Vue() 之前使用的原因浅析
2019/08/26 Javascript
python字符串替换示例
2014/04/24 Python
详解Django中的过滤器
2015/07/16 Python
使用tensorflow DataSet实现高效加载变长文本输入
2020/01/20 Python
PyInstaller的安装和使用的详细步骤
2020/06/02 Python
深入浅析pycharm中 Make available to all projects的含义
2020/09/15 Python
详解matplotlib中pyplot和面向对象两种绘图模式之间的关系
2021/01/22 Python
利用css3-animation实现逐帧动画效果
2016/03/10 HTML / CSS
纯html5+css3下拉导航菜单实现代码
2013/03/18 HTML / CSS
美国购买和销售礼品卡平台:Raise
2017/01/13 全球购物
世界上获奖最多的手机镜头:Olloclip
2018/03/03 全球购物
个人工作主要事迹
2014/05/08 职场文书
高中生旷课检讨书
2014/10/08 职场文书
2014年采购工作总结
2014/11/20 职场文书
初中差生评语
2014/12/29 职场文书
2015年高中班主任工作总结
2015/04/30 职场文书
七一慰问简报
2015/07/20 职场文书
JavaScript展开运算符和剩余运算符的区别详解
2022/02/18 Javascript