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)

defdiffer_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进行处理
  foriinrange(len(list1)):#用FOR循环对列表1进行遍历
    tag=0
    iflist1[i].find('-')>0:#对含范围的数字进行处理,放到list_yes_列表中 
      strlist=list1[i].split('-')
    list1_yes_=range(int(strlist[0]),int(strlist[1])
1)#让其生成一个范围列表
    foreachinlist1_yes_:    #FOR循环遍历所有符合条件的.
        [temp1.append(each)]
    else:          #对列表1中正常的数字进行处理,放到list_not_列表中
      list1_not_.append(int(list1[i]))#对列表1中进行处理,放到list_yes_   
  [temp1.append(i)foriinlist1_not_ifnotiintemp1]#去除重复项
  list1_final=sorted(temp1)#比较后,排序,并放到list1_final列表中
  print"list1_final value is:"
str(list1_final)#打印排序后最终list1_final列表
  
    
  #对列表2进行处理
  foriinrange(len(list2)):
    iflist2[i].find('-')>0:
      strlist=list2[i].split('-')
    list2_yes_=range(int(strlist[0]),int(strlist[1])
1)
    foreachinlist2_yes_:
        [temp2.append(each)]
      print"Temp2:"
str(temp2)
    else:
      list2_not_.append(int(list2[i]))
  [temp2.append(i)foriinlist2_not_ifnotiintemp2]
  list2_final=sorted(temp2)
  print"list2_final value is:"
str(list2_final)
  
  #对两个列表进行比较,得出最终比较结果.
  [temp.append(i)foriinlist2_finalifnotiinlist1_final]#比较两个列表差值.
  print"In list2 but not in
list1:%s"%(temp)#打印出列表1与列表2的差值
  iflen(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实现比较两个列表(list)范围的更多相关文章

  1. python基础--合并两个列表

    a = [1,2,3] b = [4,5,6] # 将两个列表合并 # append方法是不行的 # a.append(b) # print(a) # [1, 2, 3, [4, 5, 6]] # 我 ...

  2. Python中的两种列表

    python中有两种类型的列表:其中一种是用[]创建的列表,这种列表具有伸缩性,可以动态改变,而另外一种列表是用()创建,成为元组,元组一旦创建,在任何状况下都不能再改变,是一种常量列表. movie ...

  3. 如何在python中把两个列表的各项分别合并为列表

    [ [a,b] for a,b in zip(list1,list2)] 生成一个以列表list1,list2各项合并列表为元素的列表

  4. python类中两个列表实例如何相加或相减

    如下 import numpy a = [1, 2, 3, 4] b = [5, 6, 7, 8] a_array = numpy.array(a) b_array = numpy.array(b) ...

  5. python 如何在一个for循环中遍历两个列表

    是我在看<笨方法学python>过程中发现有一行代码看不懂--" for sentence in snippet, phrase:",所以研究了半天,感觉挺有收获的.所 ...

  6. 让 Python 的1、数据库查询返回字典记录--- 2、利用zip函数将两个列表(list)组成字典(dict)

    让 Python 的数据库查询返回字典记录: https://yanbin.blog/python-database-query-return-dictionary-result/#more-9179 ...

  7. [译]在Python中如何使用额enumerate 和 zip 来迭代两个列表和它们的index?

    enumerate - 迭代一个列表的index和item <Python Cookbook>(Recipe 4.4)描述了如何使用enumerate迭代item和index. 例子如下: ...

  8. 【python比较两个列表list】 python2 cmp() python3 operator模块

    https://www.yiibai.com/python/list_cmp.html 如果元素的类型相同,则执行比较并返回结果.如果元素是不同的类型,则检查它们是否是数字. 如果数字,必要时进行数字 ...

  9. python比较两个列表

    两个列表,随机产生4个不相等的数,计算一下,相同位置上的元素相等的个数,用k1表示. b列表中的元素在a列表中,但位置不相同,有多少个,用k2表示. 例如: a=[0, 4, 7, 3]b=[7, 1 ...

随机推荐

  1. 神奇的AI:将静态图片转为3D动图

    近日我们从外媒获得消息,位于莫斯科的三星AI中心和Skolkovo科学技术研究所的研究人员发表了一篇新论文,详细介绍了从单个静止人像照片生成3D动画人像的创建.与此前能够生成照片般逼真肖像的人工智能A ...

  2. idea 导出可以直接运行的jar 文件

    刚开始采用的maven插件是 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId ...

  3. SSM框架中IoC、DI与AOP的理解

    框架封装了普通项目中程序员需要重复书写的代码和调用过程,就比如说在传统的jsp项目中,我们的controller接收到前端的请求然后程序员就需要去开发Dao层,里面还涉及数据库的连接和存储过程的代码, ...

  4. 【JZOJ1282】打工

    题目 分析 显然,有一个结论, 在有效的方案中,第i位的数一定小于等于i. 所以,设\(f_{i,j,k}\)表示,做到第i位,前i位的最大值为j,前i位是否与输入的序列的前i位相等. 转移方程随便搞 ...

  5. 对vue源码之缓存的研究--------------引用

    探索vue源码之缓存篇 一.从链表说起 首先我们来看一下链表的定义: 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个 ...

  6. mysql增删改查相关操作

    mysql增删改查相关操作 以前用mysql用的少,对于数据库相关的操作不熟悉,现在开始要接触数据库了,记录一下相关的基础操作吧. 1.数据库的授权操作 # mysql -u root -p Ente ...

  7. opengl中相关的计算机图形变换矩阵之:齐次坐标 (摘编)

    模型视图变换(几何变换)矩阵: 1. 齐次坐标:两条平行线也可以相交. 在欧几里得空间中,两条平行线是无法相交的,但是在投影空间(Projective Space)这条定理就不再适用了. 比如上图中, ...

  8. lines

    lines Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. 在windows窗口下打开文件夹

    System.Diagnostics.Process.Start("Explorer.exe", @"C:\Users\gnt-wangt\Documents\Visua ...

  10. Spotlight_on_windows 安装和监控

    一.下载 1.下载并安装Spotlight_on_windows 2.填写注册码 Authorization Key:295713336449229168750 Site Message:Bergel ...