Pyt*_*234 0 python dictionary list-comprehension python-2.7
我有两个列表,包括相同的字典,格式为:
清单1:
[{'url': u'http://www.bloomberg.com/news/articles/2016-08-17/you-can-get-a-50-phone-from-amazon-if-you-don-t-mind-the-ads','title': u'You Can Get a $50 Phone From Amazon, If You Don\u2019t Mind the Ads'}, {'url': u'http://www.bloomberg.com/news/features/2016-08-18/uber-s-first-self-driving-fleet-arrives-in-pittsburgh-this-month-is06r7on', 'title': u'Uber\u2019s First Self-Driving Fleet Arrives in Pittsburgh This Month'}]
Run Code Online (Sandbox Code Playgroud)
清单2:
[{'url': u'http://www.bloomberg.com/news/articles/2016-08-17/you-can-get-a-50-phone-from-amazon-if-you-don-t-mind-the-ads', 'title': u'You Can Get a $50 Phone From Amazon, If You Don\u2019t Mind the Ads'}]
Run Code Online (Sandbox Code Playgroud)
我想做什么:我想删除list2中列表1中的字典(url和title).
我试过以下,
list1[:] = [d for d in list1 if d.get('title') != (fail for fail in list2 if fail.get('title'))]
Run Code Online (Sandbox Code Playgroud)
但无法做到这一点
预期结果:
list1 = [{'url': u'http://www.bloomberg.com/news/features/2016-08-18/uber-s-first-self-driving-fleet-arrives-in-pittsburgh-this-month-is06r7on', 'title': u'Uber\u2019s First Self-Driving Fleet Arrives in Pittsburgh This Month'}]
Run Code Online (Sandbox Code Playgroud)
只做一个简单的比较:
>>> final = [i for i in one if i not in two]
>>> final
[{'url': u'http://www.bloomberg.com/news/features/2016-08-18/uber-s-first-self-driving-fleet-arrives-in-pittsburgh-this-month-is06r7on', 'title': u'Uber\u2019s First Self-Driving Fleet Arrives in Pittsburgh This Month'}]
Run Code Online (Sandbox Code Playgroud)
list1 = final
如果你真的想要,你可以做.
归档时间: |
|
查看次数: |
37 次 |
最近记录: |