如何从 python 中的旧字典创建新的字典列表?
How to make a new list of dicts from an old one in python?
我有这个列表:
l1 = [{ 'likes': 10, 'dislikes': 1, 'title': u'sometext'}, {''likes': 7, dislikes': 5, 'title': u'other text'}]
现在我想用 score
替换 likes
和 dislkes
,这样 score = likes - dislikes
结果应该是:
l2 = [{'score': 9, 'title': u'sometext'}, {'score': 2, 'title': u'other text'}]
我想知道如何在 python 2.7 中最好地做到这一点?
l2 = [{'score': (el['likes'] - el['dislikes']), 'title': el['title']} for el in l1]
我有这个列表:
l1 = [{ 'likes': 10, 'dislikes': 1, 'title': u'sometext'}, {''likes': 7, dislikes': 5, 'title': u'other text'}]
现在我想用 score
替换 likes
和 dislkes
,这样 score = likes - dislikes
结果应该是:
l2 = [{'score': 9, 'title': u'sometext'}, {'score': 2, 'title': u'other text'}]
我想知道如何在 python 2.7 中最好地做到这一点?
l2 = [{'score': (el['likes'] - el['dislikes']), 'title': el['title']} for el in l1]