我有这个清单:
l1 = [{ 'likes': 10, 'dislikes': 1, 'title': u'sometext'}, {''likes': 7, dislikes': 5, 'title': u'other text'}]
现在我想用likes
替换dislkes
和score
,以便score = likes - dislikes
结果应为:
l2 = [{'score': 9, 'title': u'sometext'}, {'score': 2, 'title': u'other text'}]
我想知道如何在python 2.7中做到最好?
答案 0 :(得分:4)
l2 = [{'score': (el['likes'] - el['dislikes']), 'title': el['title']} for el in l1]