Python实现比较两个列表(list)范围
作者:junjie 发布时间:2022-07-20 18:08:04
有一道题: 比较两个列表范围,如果包含的话,返回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. 其次是对列表遍历的操作,同样挺重要。
猜你喜欢
- 在主图中背景颜色不知道怎么改,plt.plot()中没有axisbg参数。但是子图可以对plt.subplot的参数做修改,下面是对子图的背
- 下面的代码是日期函数的一些简单运用,应该不用解释,生成当月的日历,当然你可以根据实际情况进行扩充!效果图:<%@LANGUAGE=&q
- 本文为大家分享了php运行环境搭建安装图文教程,供大家参考,具体内容如下安装apache:1,不要安装到有中文的目录中:2,尽量将apach
- 这里列出了javascript 中的document.execCommand() 的各种参数说明:2D-Position 允许通过
- 如何显示数据库的结构?<html><head><meta http-equiv="Cont
- 下面的代码是从kesion系统扒下的,确实不错,支持utf8格式。代码如下:'===========================
- 面临的问题在我设计一个分析系统中,我们公司的目标是能够处理来自数百万个端点的大量POST请求。web 网络处理程序将收到一个JSON文档,其
- 很多时候关心的是优化SELECT 查询,因为它们是最常用的查询,而且确定怎样优化它们并不总是直截了当。相对来说,将数据装入数据库是直截了当的
- 在网络浏览器软件中,可以Internet Explorer (IE)现在是一种标准的软件。可以看到,运行不同版本的Windows操作系统(和
- 一般用 createProcessingInstruction 方法创建处理指令指定参数为 "xml","ve
- 如果你计划将数据库从SQL Server 2000 升级到 SQL Server 2005。你在升级之前一定会测试每样东西,并且证明应用程序
- 这个类主要解决在类型转换时,如果直接使用类型转换函数,会因为变量为空或者格式不对而导致程序报错,而这种报错在大多数情况下是允许的.例如要转换
- Insert 和 Update假设现在你要把下面的数据插入到数据库中.ID = 3TheDate=mktime(0,0,0,8,31,200
- 1、远程登录到linux上,使用到的模块paramiko#远程登陆操作系统def ssh(sys_ip,username,password,
- 初学python,对python的对齐很重视,为了防止出错,使用spyder工具提供的功能下面是方法:1、首先打开Tools菜单栏下的Pre
- 本文实例总结了PHP常用字符串操作函数。分享给大家供大家参考,具体如下:/*常用的字符串输出函数** echo() 输出字符串* print
- Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '+'.
- 1.什么是接口接口就是一种规范与标准,在生活中经常见接口,例如:笔记本电脑的USB接口,可以将任何厂商生产的鼠标与键盘,与电脑进行链接。为什
- 本文实例讲述了php版银联支付接口开发的方法。分享给大家供大家参考,具体如下:支付接口现在有第三方的支付接口也有银行的支付接口。这里就来介绍
- 做为一个编程爱好者,也作为一个小站长(asp之家),中国站长站(www.chinaz.com)我时不时的都会去灌一下。当然发现好的文章我也不