在列表上定义'in'运算符 [英] define 'in' operator on lists
问题描述
大家好,
我想在列表中定义''in''运算符(或任何魔术函数)
返回True如果所有列表(即_list)元素以相同的顺序出现在其他
列表(即L)中,则返回值,否则返回False。举例来说:
L = [1,2,3,4,5,6,7]
_list = [3,4,5] ]
如果L2在L0:
返回True
else:
返回False
怎么做?
-
.. :: sjf :: ..
" Linux就像Wigwam。没有盖子,没有窗户......阿帕奇里面; - )"
Hi all,
I want to define ''in'' operator (or any "magic" function) on list which
returns True value if all of list (i.e. _list) elements appears in other
list (i.e. L) in the same order and False otherwise. By way of example:
L = [1, 2, 3, 4, 5, 6, 7]
_list = [3, 4, 5]
if L2 in L0:
return True
else:
return False
How to do that?
--
..:: sjf ::..
"Linux is like Wigwam. No gates, no windows... Apache inside ;-)"
推荐答案
... :: sjf :: ..写道:
...:: sjf ::.. wrote:
我想在列表中定义''in''运算符(或任何魔术函数),如果所有列表(即_list)元素都出现,则
返回True值其他
列表(即L)的顺序相同,否则为False。
I want to define ''in'' operator (or any "magic" function) on list which
returns True value if all of list (i.e. _list) elements appears in other
list (i.e. L) in the same order and False otherwise.
怎么样 :
def containedinsequence(short,long):
ilong = iter(long)
for s简而言之:
for l in ilong:
如果s == l:
break
else:
返回False#跑出长名单
返回True
L = [1,2,3,4,5,6]
containedinsequence([3,4,6],L)
真实的含有序列([3,4,7],L)
假包含的序列([3,4,3],L)
False containedinsequence([],L)
True
def containedinsequence(short, long): ilong = iter(long)
for s in short:
for l in ilong:
if s==l:
break
else:
return False # ran out of long list
return True
L = [1, 2, 3, 4, 5, 6]
containedinsequence([3, 4, 6], L) True containedinsequence([3, 4, 7], L) False containedinsequence([3, 4, 3], L) False containedinsequence([], L) True
pewnego dnia niejaki(a)Duncan Booth wstukal(a)byl(a)co nastepuje ...:
pewnego dnia niejaki(a) Duncan Booth wstukal(a) byl(a) co nastepuje...:
.. :: sjf :: ..写道:
..:: sjf ::.. wrote:
我想定义''in''运算符(或任何魔术)如果所有列表(即_list)元素以相同的顺序出现在其他
列表(即L)中,则返回True值的列表上的函数),否则返回False。
I want to define ''in'' operator (or any "magic" function) on list which
returns True value if all of list (i.e. _list) elements appears in other
list (i.e. L) in the same order and False otherwise.
怎么样 :
How about:
def containedinsequence(短,长):
ilong = iter(长)
简而言之:
for in lilong:
如果s == l:
打破
其他:
返回False #跑出长名单
返回True
L = [1,2,3,4,5,6]
containedinsequence([3,4,6],L )
def containedinsequence(short, long):
ilong = iter(long)
for s in short:
for l in ilong:
if s==l:
break
else:
return False # ran out of long list
return True
L = [1, 2, 3, 4, 5, 6]
containedinsequence([3, 4, 6], L)
好的,这几乎是我所期待的,但我想要如果
包含序列([ 3,4,5],L)返回True,但
包含序列([3,4,6],L)返回False
,因为该序列不存在于longlist中确切地说
-
.. :: sjf :: ..
" Linux就像Wigwam。没有大门,没有窗户...... Apache内部; - )"
OK, this is nearly what I am expecting, but I want if
containedsequence([3, 4, 5], L) returns True, but
containedsequence([3, 4, 6], L) returns False
because that sequence not exist in longlist exactly
--
..:: sjf ::..
"Linux is like Wigwam. No gates, no windows... Apache inside ;-)"
2004年10月11日星期一上午11:53:36 + 0200,.. :: sjf :: ..写道:
On Mon, Oct 11, 2004 at 11:53:36AM +0200, ..:: sjf ::.. wrote:
大家好,
我想在列表中定义''in''运算符(或任何魔术函数)
列表(即L)中,则返回True值,否则返回False。举例来说:
L = [1,2,3,4,5,6,7]
_list = [3,4,5]
>如果L0中的L2:
返回True
否则:
返回False
如何做到这一点?
Hi all,
I want to define ''in'' operator (or any "magic" function) on list which
returns True value if all of list (i.e. _list) elements appears in other
list (i.e. L) in the same order and False otherwise. By way of example:
L = [1, 2, 3, 4, 5, 6, 7]
_list = [3, 4, 5]
if L2 in L0:
return True
else:
return False
How to do that?
您可能会发现''sets''模块可以满足您的需求。
You might find that the ''sets'' module does what you need.
这篇关于在列表上定义'in'运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!