如何在python中创建旧的dicts列表?

时间:2015-10-15 23:09:35

标签: python python-2.7 list-comprehension

我有这个清单:

l1 = [{ 'likes': 10, 'dislikes': 1, 'title': u'sometext'}, {''likes': 7, dislikes': 5, 'title': u'other text'}]

现在我想用likes替换dislkesscore,以便score = likes - dislikes

结果应为:

l2 = [{'score': 9, 'title': u'sometext'}, {'score': 2, 'title': u'other text'}]

我想知道如何在python 2.7中做到最好?

1 个答案:

答案 0 :(得分:4)

l2 = [{'score': (el['likes'] - el['dislikes']), 'title': el['title']} for el in l1]