简体   繁体   English

词典理解列表 - 多个条件

[英]list of dictionaries comprehension - multiple conditions

I have a problem with multiple conditions with list: 我有一个列表的多个条件的问题:

listionary = [{u'city': u'paris', u'id': u'1', u'name': u'paul'},
              {u'city': u'madrid', u'id': u'2', u'name': u'paul'},
              {u'city': u'berlin', u'id': u'3', u'name': u'tom'},
              {u'city': u'madrid', u'id': u'4', u'name': u'tom'}]

I try to delete items that meet both conditions simultaneously. 我尝试同时删除同时满足这两个条件的项目。

[elem for elem in listionary if (elem.get('name')!='paul' and elem.get('city')!='madrid')]

In this case element is removed if meet at least one condition, I try to do it in several ways, any ideas? 在这种情况下,如果满足至少一个条件,则删除元素,我尝试以多种方式进行,任何想法?

Expected output: 预期产量:

[{u'city': u'paris', u'id': u'1', u'name': u'paul'}
{u'city': u'berlin', u'id': u'3', u'name': u'tom'}
{u'city': u'madrid', u'id': u'4', u'name': u'tom'}]

I would like to remove element which meet both conditions. 我想删除符合这两个条件的元素。

Try changing the and to or . 尝试更改and to or

[elem for elem in listionary if (elem.get('name')!='paul' or elem.get('city')!='madrid')]

Remember de morgan's laws . 记住de morgan的定律 Informally: when you negate a boolean expression, you have to switch and with or in addition to switching "==" with "!=". 通俗地说:当你否定布尔表达式,你必须选择and使用or除了开关“==”和“!=”。

Your condition should have been like this 你的病情应该是这样的

[e for e in data if not (e.get('name') == 'paul' and e.get('city') == 'madrid')]

Output 产量

[{u'city': u'paris', u'id': u'1', u'name': u'paul'},
 {u'city': u'berlin', u'id': u'3', u'name': u'tom'},
 {u'city': u'madrid', u'id': u'4', u'name': u'tom'}]

This checks if the current element's name is paul and city is madrid . 这将检查当前元素的name是否为paulcity是否为madrid If both the conditions are satisfied, the not outside will flip it, so you will get False and the item will be omitted. 如果两个条件都满足, not在外面将翻转它,因此您将获得False并且项目将被省略。

Basically you are checking if the current item is the one which you don't want and make the if condition fail if it is. 基本上,您正在检查当前项目是否是您不想要的项目,如果是, if使if条件失败。

You could use itemgetter to get all the values into a tuple and then compare that: 您可以使用itemgetter将所有值转换为tuple ,然后比较:

>>> from operator import itemgetter
>>> name_and_city = itemgetter('name', 'city')
>>> [e for e in listionary if name_and_city(e) != ('paul', 'madrid')]

Your data is in fact quite static. 您的数据实际上是非常静态的。 In this case you can use namedtuple for performance on large data. 在这种情况下,您可以使用namedtuple来提高大数据的性能。

from collections import namedtuple
Profile = namedtuple('Profile', ['city', 'id', 'name'])
listionary = [Profile(*d) for d in listionary]

To improve readability, you can refactor the condition as a lambda expression (this assumes you are using namedtuple ): 为了提高可读性,您可以将条件重构为lambda表达式(假设您使用的是namedtuple ):

removed = lambda ele: \
   ele.name == 'paul' or \
   ele.city == 'madrid'
output = [ele for ele in listionary if not removed(ele)]

I think this is easier to maintain and more readable, but it may depends on who is looking at it. 我认为这更容易维护,更易读,但可能取决于谁在看它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM