我正在处理词典列表,例如:
mylist = [
{'score':11, 'type':'dftz'},
{'score':15, 'type':'dftz'},
{'score': 8, 'type':'xcdt'},
{'score': 3, 'type':'xcdt'}
]
我想:
在python3中最简单,最快的方法是什么?目前,我通过一个循环来处理它,其中每个成员都经过反复测试。有没有更“美丽”的方式呢?
答案 0 :(得分:3)
“美丽”当然是主观的,但我会使用如下列表理解:
mylist = [
{'score':11, 'type':'dftz'},
{'score':15, 'type':'dftz'},
{'score': 8, 'type':'xcdt'},
{'score': 3, 'type':'xcdt'}
]
scores = [x['score'] for x in mylist]
indexes = [i for i, x in enumerate(scores) if x>10]
退出:
>>> scores
[11, 15, 8, 3]
>>> indexes
[0, 1]