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


Posted in Python onJune 12, 2015

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

Python 相关文章推荐
Python按行读取文件的实现方法【小文件和大文件读取】
Sep 19 Python
详解用Python处理HTML转义字符的5种方式
Dec 27 Python
python正则实现提取电话功能
Feb 24 Python
Pycharm 设置自定义背景颜色的图文教程
May 23 Python
django模板结构优化的方法
Feb 28 Python
python实现用类读取文件数据并计算矩形面积
Jan 18 Python
python GUI库图形界面开发之PyQt5切换按钮控件QPushButton详细使用方法与实例
Feb 28 Python
PyCharm MySQL可视化Database配置过程图解
Jun 09 Python
keras使用Sequence类调用大规模数据集进行训练的实现
Jun 22 Python
Python 创建TCP服务器的方法
Jul 28 Python
在Python中实现字典反转案例
Dec 05 Python
python实现语音常用度量方法的代码详解
May 25 Python
在Linux系统上安装Python的Scrapy框架的教程
Jun 11 #Python
Python语言实现机器学习的K-近邻算法
Jun 11 #Python
在Linux下使用Python的matplotlib绘制数据图的教程
Jun 11 #Python
python中的代码编码格式转换问题
Jun 10 #Python
python实现数独算法实例
Jun 09 #Python
python中的全局变量用法分析
Jun 09 #Python
python简单实现计算过期时间的方法
Jun 09 #Python
You might like
JavaScript和ActionScript的交互实现代码
2010/08/01 Javascript
node.js中的fs.chmodSync方法使用说明
2014/12/18 Javascript
使用AJAX实现Web页面进度条的实例分享
2016/05/06 Javascript
js实现页面a向页面b传参的方法
2016/05/29 Javascript
Node.js如何自动审核团队的代码
2016/07/20 Javascript
JavaScript组合模式学习要点
2016/08/26 Javascript
基于JavaScript实现跳转提示页面
2016/09/24 Javascript
简单谈谈关于 npm 5.0 的新坑
2017/06/08 Javascript
Vue上传组件vue Simple Uploader的用法示例
2017/08/25 Javascript
bootstrap treeview 扩展addNode方法动态添加子节点的方法
2017/11/21 Javascript
妙用缓存调用链实现JS方法的重载
2018/04/30 Javascript
Js中使用正则表达式验证输入是否有特殊字符
2018/09/07 Javascript
vue中监听返回键问题
2019/08/28 Javascript
JS函数进阶之prototy用法实例分析
2020/01/15 Javascript
JavaScript实现PC端横向轮播图
2020/02/07 Javascript
JavaScript实现弹出窗口效果
2020/12/09 Javascript
js基于canvas实现时钟组件
2021/02/07 Javascript
[01:46]2018完美盛典章节片——坚守
2018/12/17 DOTA
用于统计项目中代码总行数的Python脚本分享
2015/04/21 Python
Python中的Descriptor描述符学习教程
2016/06/02 Python
python 实现一个贴吧图片爬虫的示例
2017/10/12 Python
Python编程使用*解包和itertools.product()求笛卡尔积的方法
2017/12/18 Python
Python实现PS滤镜功能之波浪特效示例
2018/01/26 Python
基于Python3.6+splinter实现自动抢火车票
2018/09/25 Python
使用python将图片格式转换为ico格式的示例
2018/10/22 Python
python3实现斐波那契数列(4种方法)
2019/07/15 Python
大学军训感言1000字
2014/02/25 职场文书
《学会合作》教学反思
2014/04/12 职场文书
节约能源标语
2014/06/17 职场文书
预备党员期盼十八届四中全会召开思想汇报
2014/10/17 职场文书
班级元旦晚会开幕词
2015/01/29 职场文书
家长高考寄语
2015/02/27 职场文书
《童年的发现》教学反思
2016/02/18 职场文书
为什么node.js不适合大型项目
2021/04/28 Javascript
Mysql事务索引知识汇总
2022/03/17 MySQL
MySQL中正则表达式(REGEXP)使用详解
2022/07/07 MySQL