迭代筛选列表 [英] Iterating across a filtered list

查看:101
本文介绍了迭代筛选列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部 -


我正在写一个玩具程序,因为我学习了python,它是一个简单的地址簿。我遇到了搜索功能中的情况

我希望迭代一个已过滤的列表。我的代码正在工作

很好,但我想知道这是否是最优雅的。做的方式

这个。基本上,我正在搜索字典self.contacts以获得一个键,即
匹配用户输入的模式。如果是这样,我打印与该键相关联的值

。这个方法的方法如下,任何帮助/

建议表示赞赏:

http://pastie.caboo.se/46647


旁注:我正在学习红宝石经验之后的python。在红宝石中,我会这样做:
做类似的事情:


contacts.find_all {|姓名,联系方式| name =〜/ search /}。每个{|名称,联系人|

联系人}

解决方案

"德鲁" < ol ***** @ gmail.comwrites:


我正在写一个玩具程序,因为我学习python作为一个

简单的地址簿。我遇到了搜索功能中的情况

我希望迭代一个已过滤的列表。我的代码正在工作

很好,但我想知道这是否是最优雅的。做的方式

这个。基本上,我正在搜索字典self.contacts以获得一个键,即
匹配用户输入的模式。如果是这样,我打印与该键相关联的值

。这个方法的方法如下,任何帮助/

建议表示赞赏:



如果我能破译你的Ruby例子(我不知道)不知道Ruby),我想你

想要:


的名字,联系方式contact.iteritems():

如果re.search(''search'',名称):

打印联系人


如果你只想过滤表达式中的字典,你

可以使用生成器表达式:


d =((名称,联系人)(contacts,contact)contact.iteritems()\

如果re.search(''search'',名称))


print''\ n''。join(d)#打印过滤后的字典中的项目,每行一个


注意d是迭代器,这意味着当你通过它执行
时它会发生变异。


3月13日下午2:42,Paul Rubin< http://phr...@NOSPAM.invalidwrote:


如果我能破译你的Ruby例子(我不知道Ruby),我想你

想要:


的名字,联系方式contact.iteritems():

如果re.search(''search'',名字):

打印联系人


如果你只想过滤里面的字典表达式,你/ b $ b可以使用生成器表达式:


d =((姓名,联系方式)contact.iteritems()\中的(姓名,联系方式)

如果re.search(''search'',名称))


print''\ n''。join(d)#print items来自过滤的dict,每行一个


注意d是一个迭代器,这意味着当你通过它执行
时它会发生变异。



保罗 -


你的确是正确的。我想我只是想知道你的第一个

例子(也就是说,打破if语句远离迭代)

是首选,而不是最初过滤然后迭代。

然而,你很有意义的例子很有帮助。


谢谢,

画了


3月13日下午6:04,Drew < olso ... @ gmail.comwrote:


全部 -



嗨!


[snip]

http://pastie.caboo.se/46647



没有必要像你这样复杂的列表理解

迭代立即过来!将过滤逻辑

放在for循环中更清楚。此外,您重新计算列表中每个元素

的正则表达式。相反,我会做这样的事情:


def find(search_str,flags = re.IGNORECASE):

print"找到的联系人:" ;

search = re.compile(search_str,flags).search

for name,contact in self.contacts.items():

如果搜索(姓名):

打印联系人

打印


虽然我宁愿有一个函数返回所有列表

找到联系人:


def find(search_str,flags = re.IGNORECASE):

search = re.compile(search_str ,标志)。搜索

的名称,联系人在self.contacts.items():

如果搜索(姓名):

产品联系


然后另一个打印它。


旁注:我正在学习红宝石经验之后的python。在红宝石中,我会这样做:
做类似的事情:


contacts.find_all {|姓名,联系方式| name =〜/ search /}。每个{|姓名,联系方式|

联系人}



这就是你的原因' '学习Python的权利;)


HTH


-

Arnaud


All -

I''m currently writing a toy program as I learn python that acts as a
simple address book. I''ve run across a situation in my search function
where I want to iterate across a filtered list. My code is working
just fine, but I''m wondering if this is the most "elegant" way to do
this. Essentially, I''m searching the dict self.contacts for a key that
matches the pattern entered by the user. If so, I print the value
associated with that key. A pastie to the method is below, any help/
advice is appreciated:

http://pastie.caboo.se/46647

Side note: I''m learning python after ruby experience. In ruby I would
do something like:

contacts.find_all{|name,contact| name =~ /search/}.each{|name,contact|
puts contact}

解决方案

"Drew" <ol*****@gmail.comwrites:

I''m currently writing a toy program as I learn python that acts as a
simple address book. I''ve run across a situation in my search function
where I want to iterate across a filtered list. My code is working
just fine, but I''m wondering if this is the most "elegant" way to do
this. Essentially, I''m searching the dict self.contacts for a key that
matches the pattern entered by the user. If so, I print the value
associated with that key. A pastie to the method is below, any help/
advice is appreciated:

If I can decipher your Ruby example (I don''t know Ruby), I think you
want:

for name,contact in contacts.iteritems():
if re.search(''search'', name):
print contact

If you just want to filter the dictionary inside an expression, you
can use a generator expression:

d = ((name,contact) for (name,contact) in contacts.iteritems() \
if re.search(''search'', name))

print ''\n''.join(d) # prints items from filtered dict, one per line

Note that d is an iterator, which means it mutates when you step
through it.


On Mar 13, 2:42 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:

If I can decipher your Ruby example (I don''t know Ruby), I think you
want:

for name,contact in contacts.iteritems():
if re.search(''search'', name):
print contact

If you just want to filter the dictionary inside an expression, you
can use a generator expression:

d = ((name,contact) for (name,contact) in contacts.iteritems() \
if re.search(''search'', name))

print ''\n''.join(d) # prints items from filtered dict, one per line

Note that d is an iterator, which means it mutates when you step
through it.

Paul -

You''re exactly on the mark. I guess I was just wondering if your first
example (that is, breaking the if statement away from the iteration)
was preferred rather than initially filtering and then iterating.
However, you''re examples make a lot of sense are are quite helpful.

Thanks,
Drew


On Mar 13, 6:04 pm, "Drew" <olso...@gmail.comwrote:

All -

Hi!

[snip]

http://pastie.caboo.se/46647

There is no need for such a convoluted list comprehension as you
iterate over it immediately! It is clearer to put the filtering logic
in the for loop. Moreover you recalculate the regexp for each element
of the list. Instead I would do something like this:

def find(search_str, flags=re.IGNORECASE):
print "Contact(s) found:"
search = re.compile(search_str, flags).search
for name, contact in self.contacts.items():
if search(name):
print contact
print

Although I would rather have one function that returns the list of all
found contacts:

def find(search_str, flags=re.IGNORECASE):
search = re.compile(search_str, flags).search
for name, contact in self.contacts.items():
if search(name):
yield contact

And then another one that prints it.

Side note: I''m learning python after ruby experience. In ruby I would
do something like:

contacts.find_all{|name,contact| name =~ /search/}.each{|name,contact|
puts contact}

And that''s why you''re right to learn Python ;)

HTH

--
Arnaud


这篇关于迭代筛选列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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