如何使别人在嵌套语句列表理解返回任何结果

问题描述 投票:1回答:2

我想else语句在我的列表理解任何回报 - 即我想要的只有2输出,如果在我的代码语句。我怎么能这样做吗?

数据:

pleasant_sounding = ['Farm', 'Park', 'Hill', 'Green']

royal_sounding = ['Royal', 'Queen', 'King']

location_dict['Outer London'] = ['Brent Park',  'Woodford', 'Dollis Hill', 'Park Royal',  'Redbridge']

我的代码:

    [ '{} sounds pleasant'.format(name) 
if any(x in name for x in pleasant_sounding)
else '{} sounds grand'.format(name) 
if any(y in name for y in royal_sounding)
else '' for name in location_dict['Outer London'] ]

我的输出:

布伦特公园愉快的声音

''

Dollis山愉快的声音

皇家公园隆重的声音

''

预期输出:

布伦特公园愉快的声音

Dollis山愉快的声音

皇家公园隆重的声音

python-2.7 list-comprehension
2个回答
1
投票

您还可以在列表内涵添加if ...您的代码可以生成你要寻找的一些简化和少量的添加结尾:

['{} sounds pleasant'.format(name) if any(x in name for x in pleasant_sounding)
 else '{} sounds grand'.format(name)
 for name in location_dict['Outer London']
 if any(x in name for x in pleasant_sounding+royal_sounding)]

换句话说只是三元表达,与包括滤波条件的理解

[<X> if <condition> else <Y>
 for <var> in <container>
 if <test>]

1
投票

您的列表理解返回

['Brent Park sounds pleasant', '', 'Dollis Hill sounds pleasant', 'Park Royal sounds pleasant', '']

你只需要过滤它:

>>> [t for t in <your list comprehension here> if t != '' ]
['Brent Park sounds pleasant', 'Dollis Hill sounds pleasant', 'Park Royal sounds pleasant']

那是:

>>> [t for t in ('{} sounds pleasant'.format(name)
... if any(x in name for x in pleasant_sounding)
... else '{} sounds grand'.format(name)
... if any(y in name for y in royal_sounding)
... else '' for name in location_dict['Outer London']) if t != '' ]
['Brent Park sounds pleasant', 'Dollis Hill sounds pleasant', 'Park Royal sounds pleasant']

我使用的发电机(注意括号)的内部,因为我们并不需要构建列表中,但仅仅是一种评估值一个接一个。该代码仍不清楚,因为你有,在列表中理解,一个复杂的表达式创建字符串返回的中间。您应该使用的函数:

>>> def text(name):
...     if any(x in name for x in pleasant_sounding):
...         return '{} sounds pleasant'.format(name)
...     elif any(y in name for y in royal_sounding):
...         return '{} sounds grand'.format(name)
...     return None # None is better than '' here
... 
>>> [t for t in (text(name) for name in location_dict['Outer London']) if t is not None ]
['Brent Park sounds pleasant', 'Dollis Hill sounds pleasant', 'Park Royal sounds pleasant']

如果你愿意,你可以使用一个更实用的风格:

>>> list(filter(None, map(text, location_dict['Outer London'])))
['Brent Park sounds pleasant', 'Dollis Hill sounds pleasant', 'Park Royal sounds pleasant']

我还看到你if any(name ...)测试一些冗余。想象一下,你有很多冠冕堂皇的类型:你的代码会变得繁琐的维护。你可以使用一个更常用的方法:

>>> soundings = [("pleasant", ['Farm', 'Park', 'Hill', 'Green']), ("grand", ['Royal', 'Queen', 'King'])}
>>> def text(name):
...     for sounding_type, substrings in soundings:
...         if any(x in name for x in substrings):
...             return '{} sounds {}'.format(name, sounding_type)
...     return None
... 
>>> [t for t in (text(name) for name in location_dict['Outer London']) if t is not None]
['Brent Park sounds pleasant', 'Dollis Hill sounds pleasant', 'Park Royal sounds pleasant']

注:这是Python的3.7,但可以将其调整到Python 2.7(iteritems而不是items)。

© www.soinside.com 2019 - 2024. All rights reserved.