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)
list2):
44-67
如:[1,2,3,4,5,6,7,8,9,10]
44-67
如:[1,2,3,4,5,6,7,8,9,10]
1)#让其生成一个范围列表
str(list1_final)#打印排序后最终list1_final列表
1)
str(temp2)
str(list2_final)
list1:%s"%(temp)#打印出列表1与列表2的差值
result is: False"
result is: True"
总结:
1. 这道题关键是想法,如果整成坐标的方式来比较,会很麻烦。
2. 列表转成范围后,如果消除重复项,同样是里面的关键所在。
3. 其次是对列表遍历的操作,同样挺重要。
Python实现比较两个列表(list)范围的更多相关文章
- python基础--合并两个列表
a = [1,2,3] b = [4,5,6] # 将两个列表合并 # append方法是不行的 # a.append(b) # print(a) # [1, 2, 3, [4, 5, 6]] # 我 ...
- Python中的两种列表
python中有两种类型的列表:其中一种是用[]创建的列表,这种列表具有伸缩性,可以动态改变,而另外一种列表是用()创建,成为元组,元组一旦创建,在任何状况下都不能再改变,是一种常量列表. movie ...
- 如何在python中把两个列表的各项分别合并为列表
[ [a,b] for a,b in zip(list1,list2)] 生成一个以列表list1,list2各项合并列表为元素的列表
- python类中两个列表实例如何相加或相减
如下 import numpy a = [1, 2, 3, 4] b = [5, 6, 7, 8] a_array = numpy.array(a) b_array = numpy.array(b) ...
- python 如何在一个for循环中遍历两个列表
是我在看<笨方法学python>过程中发现有一行代码看不懂--" for sentence in snippet, phrase:",所以研究了半天,感觉挺有收获的.所 ...
- 让 Python 的1、数据库查询返回字典记录--- 2、利用zip函数将两个列表(list)组成字典(dict)
让 Python 的数据库查询返回字典记录: https://yanbin.blog/python-database-query-return-dictionary-result/#more-9179 ...
- [译]在Python中如何使用额enumerate 和 zip 来迭代两个列表和它们的index?
enumerate - 迭代一个列表的index和item <Python Cookbook>(Recipe 4.4)描述了如何使用enumerate迭代item和index. 例子如下: ...
- 【python比较两个列表list】 python2 cmp() python3 operator模块
https://www.yiibai.com/python/list_cmp.html 如果元素的类型相同,则执行比较并返回结果.如果元素是不同的类型,则检查它们是否是数字. 如果数字,必要时进行数字 ...
- python比较两个列表
两个列表,随机产生4个不相等的数,计算一下,相同位置上的元素相等的个数,用k1表示. b列表中的元素在a列表中,但位置不相同,有多少个,用k2表示. 例如: a=[0, 4, 7, 3]b=[7, 1 ...
随机推荐
- redis数据库到mysql或mongodb数据库
# -*- coding:utf-8 -*-# item_mongodb.py import redis import pymongo import json def main(): redis_co ...
- Scrapy-redis分布式+Scrapy-redis实战
[学习目标] Scrapy-redis分布式的运行流程 Scheduler与Scrapy自带的Scheduler有什么区别 Duplication Filter作用 源码自带三种spider的使用 6 ...
- PAT Basic 1076 Wifi密码 (15 分)
下面是微博上流传的一张照片:“各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B-2:C-3:D-4:请同学们自己作答,每两日一 ...
- Binary Numbers AND Sum CodeForces - 1066E (前缀和)
You are given two huge binary integer numbers aa and bb of lengths nn and mmrespectively. You will r ...
- JS获取当前日期和时间的方法,并按照YYYY-MM-DD格式化
Js获取当前日期时间及其它操作 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); ...
- 重大更新:DeepFaceLab更新至2019.12.20
本次更新增加SAEHD:lr_dropout参数,训练时可以打开或者禁用(默认禁用),每次换脸经过足够的训练后可以启用此选项以减少重复次数,从而获得额外的清晰度.还有一个比较有意义的更新是增加了图片元 ...
- /etc/fstab 下的配置参数
第一列:分区的label或者UUID 若要查看/dev/sdb1设备的label或者UUID[root@localhost ~]# dumpe2fs -h /dev/sdb1dumpe2fs 1.42 ...
- STM32CubeIDE Debug Configurations
新建完工程并编译后,配置Debug Configurations 此时双击STM32 MCU Debugging,如下图 此时就可以下载调试生成的LED.elf文件了
- 解决Eclipse中新建jsp文件ISO8859-1 编码问题
看了许多的贴说是在eclipse --> window --> Preferences --> General --> Content Types --> text--& ...
- Delphi---ShellExecute跨进程调用exe
测试环境:Delphi7 + Win7 发起端 unit uRequest; interface uses Windows, Messages, SysUtils, Variants, Classes ...