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. 其次是对列表遍历的操作,同样挺重要。

转载于:https://www.cnblogs.com/amengduo/p/9586505.html

Python实现比较两个列表(list)范围相关推荐

  1. 如何在Python中串联两个列表?

    如何在Python中串联两个列表? 例: listone = [1, 2, 3] listtwo = [4, 5, 6] 预期结果: >>> joinedlist [1, 2, 3, ...

  2. python 两个列表比较_如何在Python中比较两个列表

    python 两个列表比较 In this article, we will understand the different ways to compare two lists in Python. ...

  3. python找出两个列表不同元素_python 找出两个列表的相同元素与不同元素

    python 找出两个列表的相同元素与不同元素,用了以下几种方法 1. 通过set 集合 list1 = [1,2,3] list2=[2,3,4] set1 = set(list1) set2 = ...

  4. python中怎么比较两个列表-python中比较两个列表的实例方法

    cmp() 方法用于比较两个列表的元素. cmp()方法语法: cmp(list1, list2) 参数: list1 -- 比较的列表.list2 -- 比较的列表. 返回值: 如果比较的元素是同类 ...

  5. 【Python】将两个列表内容合并成一个字典

    将两个列表内容合并成一个字典 keys = ['A', 'B', 'C'] values = ['blue', 'red', 'bold'] 有 3 种方法可以将这两个列表转换为字典 1.使用Pyth ...

  6. python中确定两个列表(list)之间是否为子集关系

    将列表(list)转换成集合(set)作比较 >>> A = [1,2,3,4,5] >>> B = [1,2,3] >>> C = [1,2,3 ...

  7. python lambda函数两个列表大小关系_python lambda结合列表推导式?

    func1 = [lambda x:x*i for i in range(10)] [f1(2) for f1 in func1] [18, 18, 18, 18, 18, 18, 18, 18, 1 ...

  8. python 对应元素相乘_如何在Python中执行两个列表的元素相乘?

    使用与zip()混合的列表理解:. [a*b for a,b in zip(lista,listb)] 由于您已经在使用numpy ,所以将数据存储在一个numpy数组而不是列表中是很有意义的. 一旦 ...

  9. python 同时遍历两个列表并组成一个字典

    dict1 = {} # 创建空字典 list1 = ['a', 'b', 'c'] # 创建列表 list2 = ['1', '2', '3'] # 创建列表 for key, value in z ...

最新文章

  1. iOS 中socket的总结
  2. java.lang.OutOfMemoryError: Java heap space的解决办法
  3. WinAPI: SetTextAlign - 设置绘图环境的文本对齐方式
  4. 为什么ConcurrentHashMap是弱一致的(jdk6)
  5. 函数没有“as”子句;假定返回类型为 object。_TypeScript笔记(一)类型amp;接口...
  6. python中变量名后的逗号_深入浅析python变量加逗号,的含义
  7. python实现二分法查找_python二分法查找
  8. HDU2551 竹青遍野【数学计算+水题】
  9. NUC1158 Lake Counting【DFS】
  10. c3p0数据库连接池配置
  11. rp软件app流程图_Axure制作流程图的图文操作
  12. 修改UI及盒子启动及部分后续操作
  13. ES6 Javascript Nodejs Mindmap
  14. JS正则匹配实例邮箱、手机号、电话、货币
  15. 32位浮点数转十进制与二进制
  16. 为什么大数据平台要回归SQL
  17. GoLang之标准库net/http包源码
  18. 利用Java语言编写一个猜数字游戏(有次数限制)
  19. 影响IT人员未来发展的五个IT新技术方向
  20. 七夕节送女朋友啥礼物好?七夕情人节礼物推荐

热门文章

  1. 如何利用System.Net.Mail类发送EMAIL
  2. 使用jQuery queue(队列) 遇到的问题及解决方案
  3. C++没有调用析构函数
  4. [转]被当做狗和鸡来驱赶的百姓
  5. 前端测试利器--Browser-Sync启动命令
  6. 包含JS交互的混淆出错
  7. iOS开发--验证码
  8. 动手写了一个12306插件 chrome浏览器
  9. shell实现从1加到100
  10. mysql5.7.11解压版安装_Mysql5.7.11在windows10上的安装与配置(解压版)