Python实现比较两个列表(list)范围

 更新时间:2015年06月12日 08:44:51   投稿:junjie  
这篇文章主要介绍了Python实现比较两个列表(list)范围,本文根据一道题目实现解决代码,本文分别给出题目和解答源码,需要的朋友可以参考下

有一道题: 比较两个列表范围,如果包含的话,返回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. 其次是对列表遍历的操作,同样挺重要。

相关文章

  • tensorflow基于CNN实战mnist手写识别(小白必看)

    tensorflow基于CNN实战mnist手写识别(小白必看)

    这篇文章主要介绍了tensorflow基于CNN实战mnist手写识别(小白必看),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Python OpenCV使用dlib进行多目标跟踪详解

    Python OpenCV使用dlib进行多目标跟踪详解

    这篇文章主要为大家介绍了如何使用 dlib 库在实时视频中有效地跟踪多个对象,文中的示例代码讲解详细,对我们学习OpenCV有一定帮助,需要的可以参考一下
    2022-03-03
  • postman模拟访问具有Session的post请求方法

    postman模拟访问具有Session的post请求方法

    今天小编就为大家分享一篇postman模拟访问具有Session的post请求方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • Python文件操作,open读写文件,追加文本内容实例

    Python文件操作,open读写文件,追加文本内容实例

    本篇文章主要介绍了Python文件操作,open读写文件,追加文本内容,具有一定的参考价值,有需要的可以了解一下。
    2016-12-12
  • Python打印不合法的文件名

    Python打印不合法的文件名

    这篇文章主要介绍了Python打印不合法的文件名,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下的相关资料
    2020-07-07
  • python套接字socket通信

    python套接字socket通信

    这篇文章主要介绍了python套接字socket通信,python标准库中内置了底层网络接口socket,以下代码均默认from socket import *,下文很多详细内容需要的小伙伴可以参考一下
    2022-04-04
  • Django如何实现网站注册用户邮箱验证功能

    Django如何实现网站注册用户邮箱验证功能

    这篇文章主要介绍了Django如何实现网站注册用户邮箱验证功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • python paramiko利用sftp上传目录到远程的实例

    python paramiko利用sftp上传目录到远程的实例

    今天小编就为大家分享一篇python paramiko利用sftp上传目录到远程的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • 基于Python快速处理PDF表格数据

    基于Python快速处理PDF表格数据

    这篇文章主要介绍了基于Python快速处理PDF表格数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • 纯numpy卷积神经网络实现手写数字识别的实践

    纯numpy卷积神经网络实现手写数字识别的实践

    本文主要介绍了纯numpy卷积神经网络实现手写数字识别的实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08

最新评论