没有合适的资源?快使用搜索试试~ 我知道了~
首页Python实现比较两个列表(list)范围
资源详情
资源评论
资源推荐
Python实现比较两个列表实现比较两个列表(list)范围范围
主要介绍了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:
weixin_38744270
- 粉丝: 327
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0