[英]Checking for sublist in list
The Question is: You are to write a function, called isSublist()
, which takes two arguments ( list, sublist
) and returns 1
if sublist is a sub-list of list, and 0 otherwise. 问题是:您要编写一个名为
isSublist()
的函数,该函数接受两个参数( list, sublist
isSublist()
,如果sublist是list的子列表,则返回1
否则返回0。
So i have my code however I get True
when the sublist is not in list. 所以我有我的代码,但是当子列表不在列表中时我得到
True
。 Any suggestions on fixing this please? 有关解决此问题的任何建议吗?
def isSublist(list, sublist):
for i in range(len(list)-(len(sublist))+1):
return True
if sublist==list[i:i+(len(sublist))]:
return False
sample input: 样本输入:
list= (0,1,2,3,4,5,6,7,8,9)
isSublist(list, [1,2,3])
output:
True
You can break this up by getting all the slices the size of the sublist, and comparing equality: 您可以通过获取所有切片的子列表大小并比较相等性来对此进行分解:
def n_slices(n, list_):
for i in xrange(len(list_) + 1 - n):
yield list_[i:i+n]
def isSublist(list_, sub_list):
for slice_ in n_slices(len(sub_list), list_):
if slice_ == sub_list:
return True
return False
To cover the issue of ordering. 涵盖订购问题。 A list is, by definition, ordered.
根据定义,列表是有序的。 If we want to ignore ordering, you can do sets:
如果我们想忽略排序,你可以做套装:
def isSubset(list_, sub_list):
return set(sub_list) <= set(list_)
If we need to cover repeated elements and ignore ordering, you're now in the realm of multisets: 如果我们需要覆盖重复元素并忽略排序,那么您现在处于多集合领域:
def isSubset(list_, sub_list):
for item in sub_list:
if sub_list.count(item) > list_.count(item):
return False
return True
>>> def isSublist(originallist,sublist,biglist):
if not sublist:
return True
if not biglist:
return False
if sublist[0]==biglist[0]:
return isSublist(originallist,sublist[1:],biglist[1:]) or isSublist(originallist,sublist,biglist[1:])
return isSublist(originallist,originallist,biglist[1:])
test output: 测试输出:
>>> isSublist([1,2,4],[1,2,4],[1,2,3,4])
False
>>> isSublist([1,2,3],[1,2,3],[1,2,3,4])
True
>>>
Edited for sublists, not subset. 针对子列表而不是子列表进行编辑。 Uglier, but works.
Uglier,但是有效。 Could add a wrapper to avoid confusion of parameters.
可以添加包装器以避免参数混淆。
def sublist(a,b):
isSublist(a,a,b)
Part of your problem is that in Python (0,1,2,3,4,5,6,7,8,9)
isn't technically a list
, it's a tuple
-- which is essentially a immutable (unchangeable) list
. 您的问题的一部分是在Python
(0,1,2,3,4,5,6,7,8,9)
从技术上讲不是一个list
,而是一个tuple
-本质上是一个不可变(不变)的list
。 Also, you should avoid naming things in your program the same as built-in functions and types, because occasionally you need to reference them and your own definition would hide the ones provided by the system. 此外,您应该避免在程序中命名与内置函数和类型相同的内容,因为有时您需要引用它们,并且您自己的定义将隐藏系统提供的内容。
One easy way to get around this is to just appending an _
to the end of the name, as done in the following which tries to turn convert the argument into a list
if it isn't one initially. 解决此问题的一种简单方法是在名称的末尾附加一个
_
,如下所示,如果最初不是一个,则尝试将参数转换为list
。 It doesn't test the type of the sublist
argument, but a similar check might be appropriate for it, too. 它不测试
sublist
参数的类型,但类似的检查也可能适合它。
def isSublist(list_, sublist):
if not isinstance(list_, list):
list_ = list(list_)
sublen = len(sublist)
for i in xrange(len(list_)-sublen+1):
if list_[i:i+sublen] == sublist:
return True
return False
list_ = (0,1,2,3,4,5,6,7,8,9)
print subfunc(list_, [0,1]) # --> True
print subfunc(list_, [1,2,3]) # --> True
print subfunc(list_, [4,6,7]) # --> False
print subfunc(list_, [4,5]) # --> True
def isSublist(x, y):
occ = [i for i, a in enumerate(x) if a == y[0]]
for b in occ:
if x[b:b+len(y)] == y:
print 'YES-- SUBLIST at : ', b
return True
else:
pass
if len(occ)-1 == occ.index(b):
print 'NO SUBLIST'
return False
list1 = [1,0,1,1,1,0,0]
list2 = [1,0,1,0,1,0,1]
#should return True
isSublist(list1, [1,1,1])
#Should return False
isSublist(list2, [1,1,1])
I've just written a recipe for this (this works for contiguous sublist): 我刚刚为此编写了一个食谱(此方法适用于连续的子列表):
def is_sublist(sublist, superlist):
'''''
Checks whether 'sublist' is a sublist of 'superlist'.
Both arguments must be typed list or tuple.
'''
if not isinstance(sublist, (list, tuple)) or not isinstance(sublist, (list,tuple)):
raise TypeError("Both 'sublist' and 'superlist' must be lists or tuples.")
# Return early if candidate sublist is longer than superlist
if len(sublist) > len(superlist):
return False
else:
for chunk in (superlist[i:i+len(sublist)] for i in range(0, len(superlist)-len(sublist)+1)):
if chunk == sublist:
return True
return False
The idea is to have a shifting window (having the same width as the candidate sublist) scan the superlist and check for equality between the chunk and the sublist at each iteration. 想法是使移位窗口(具有与候选子列表相同的宽度)扫描超级列表并在每次迭代时检查块和子列表之间的相等性。
That's a brute force method. 那是一种蛮力方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.