Rita Sue和Bob也是 [英] Rita Sue and Bob too

查看:69
本文介绍了Rita Sue和Bob也是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


有人可以提供帮助。我保证我已经看过怎么做但却找不到

的方式...


好​​的,找一个名字很容易


如果''Bob''在列表中:

print"他们被发现了

else:

print"它们不在列表中


但是,如何在未知大小的列表中找到序列?即这个序列

以其他三个名字替换?


''Rita'',''Sue'',''Bob ''


这几乎是夜间发生(我的帖子问题),但我是

学习:)

Hi All,

Can someone help. I promise I''ve looked how to do this but can''t find a
way...

Ok, to find one name is easy

if ''Bob'' in list:
print "They were found"
else:
print "They are not in list"

But, how to I find a sequence in a list of unknown size? i.e. this sequence
in list of other names and replace it with three others?

''Rita'',''Sue'',''Bob''

This is almost a nightly occurrence (my posting questions), but I am
learning : )

推荐答案

M. Clift写道:
M. Clift wrote:
大家好,

有人可以提供帮助。我保证我已经看过如何做到这一点却无法找到一个
方式...

好的,找个名字很容易

如果''鲍勃'在列表中:
打印他们被发现
否则:
打印他们不在列表中

但是,如何我在未知大小的列表中找到序列?即这个序列在其他名字的列表中,并用其他三个替换它?

''Rita'',''Sue'',''Bob''
Hi All,

Can someone help. I promise I''ve looked how to do this but can''t find a
way...

Ok, to find one name is easy

if ''Bob'' in list:
print "They were found"
else:
print "They are not in list"

But, how to I find a sequence in a list of unknown size? i.e. this sequence
in list of other names and replace it with three others?

''Rita'',''Sue'',''Bob''

This is almost a nightly occurrence (my posting questions), but I am
learning : )




我的第一个想法是扫描清单第一个元素

''Rita'',当你找到它时,取出一块与你在那里相同大小的

(使用len(使用len) )当然)

并比较两者。如果他们不匹配,继续前进。扫描

和继续前进使用.index()

最好完成部分并注意该函数可以采用第二个参数

指定起始索引,因此您可以进行渐进式搜索

有效率。实际代码留给了

读者。 ;-)


-Peter



My first thought would be to scan the list for the first element
''Rita'', and when you find it, take out a slice that is the
same size as the sequence you have there (use len() of course)
and compare the two. If they don''t match, move on. The scanning
and "moving on" part would certainly best be done using .index()
and note that that function can take a second parameter which
specifies the starting index, so you can do a progressive search
efficiently. The actual code is left as an exercise to the
reader. ;-)

-Peter


M. Clift写道:
M. Clift wrote:
大家好,

有人可以提供帮助。我保证我已经看过如何做到这一点却无法找到一个
方式...

好的,找个名字很容易

如果''鲍勃'在列表中:
打印他们被发现
否则:
打印他们不在列表中

但是,如何我在未知大小的列表中找到序列?即这个序列在其他名字的列表中,并用其他三个替换它?

''Rita'',''Sue'',''Bob''
Hi All,

Can someone help. I promise I''ve looked how to do this but can''t find a
way...

Ok, to find one name is easy

if ''Bob'' in list:
print "They were found"
else:
print "They are not in list"

But, how to I find a sequence in a list of unknown size? i.e. this sequence
in list of other names and replace it with three others?

''Rita'',''Sue'',''Bob''

This is almost a nightly occurrence (my posting questions), but I am
learning : )



我敢肯定别人可以上来有更优雅的东西,但这是你可以做到的方式:


I''m sure someone else can come up with something more elegant, but
here''s a way you could do it:

名字
[''拉里'',''卷曲'',''moe'',''shimp'',''rita'',''sue'',''bob'', ''billy'',''scott''] idx in range(len(names)):
....如果name [idx:idx + 3] == [''sue'',' 'bob'',''billy'']:

.... print"'em'at element",idx

.... break < br $>
....

在'元素5'找到它们'


注意,这个:[''起诉,鲍勃,比利]在名称

返回false。原因是,我认为,因为集合[''起诉'',''bob'',

''billy'']并不是[''''''''''''''''''''''''''''' ''卷曲'',''moe'',''shimp'',

''rita'',''起诉'',''bob'',''billy'', ''scott''],即使三个名字

依次出现在两个列表中。但是,这个:

names2
[[''sue'',''bob'',''billy''],[''larry'',''curly'' ,''moe'',''shimp'',''rita'',

''sue'',''bob'',''billy'',''scott'' ]] [''sue'',''bob'',''billy'']在names2
True
names [''larry'', ''curly'', ''moe'', ''shimp'', ''rita'', ''sue'', ''bob'', ''billy'', ''scott''] for idx in range(len(names)): .... if names[idx:idx + 3] == [''sue'', ''bob'', ''billy'']:
.... print "found ''em at element", idx
.... break
....
found ''em at element 5
Notice, this: [''sue'', ''bob'', ''billy''] in names False returns false. The reason is, I think, because the set [''sue'', ''bob'',
''billy''] is not really a subset of [''larry'', ''curly'', ''moe'', ''shimp'',
''rita'', ''sue'', ''bob'', ''billy'', ''scott''], even though the three names
appear sequentially in both lists. But, this:
names2 [[''sue'', ''bob'', ''billy''], [''larry'', ''curly'', ''moe'', ''shimp'', ''rita'',
''sue'', ''bob'', ''billy'', ''scott'']] [''sue'', ''bob'', ''billy''] in names2 True



确实 ;工作"出于同样的原因,它在第一个

例子中没有用。列表[''sue'',''bob'',''billy'']本身是较大的

列表的一部分,名称2.


HTH,

Jeremy Jones


does "work" for the same reason that it doesn''t work in the first
example. The list [''sue'', ''bob'', ''billy''] itself is part of the larger
list, names2.

HTH,

Jeremy Jones


" M.克里福特" <无*** @ here.com>写道:
"M. Clift" <no***@here.com> writes:
但是,如何在未知大小的列表中找到序列?即这个序列在其他名字的列表中,并用其他三个替换它?

''Rita'',''Sue'',''Bob''

这几乎是每晚发生(我的帖子问题),但我学习了:)
But, how to I find a sequence in a list of unknown size? i.e. this sequence
in list of other names and replace it with three others?

''Rita'',''Sue'',''Bob''

This is almost a nightly occurrence (my posting questions), but I am
learning : )




你必须浏览列表并寻找那个序列,例如

$ x $ b for x in xrange(len(mylist) - 2):

如果是mylist [i:i + 3] == [' '丽塔'',''苏',''鲍勃'']:

打印''他们被发现''

休息

有更好的和更有效的方法来扫描列表,但这是基本的想法。



You have to scan through the list and look for that sequence, e.g.

for i in xrange(len(mylist) - 2):
if mylist[i:i+3] == [''Rita'',''Sue'',''Bob'']:
print ''They were found''
break

There are fancier and more efficient ways to scan the list, but this
is the basic idea.


这篇关于Rita Sue和Bob也是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆