python的dict查找值列表以

时间:2014-01-22 18:48:44

标签: python list dictionary

嗨,我有一个dict列表

Player_list = [{'FirstName': 'Rad', 'AId': 178518, 'LastName': 'Nain'},
               {'FirstName': 'Lad', 'AId': 178519, 'LastName': 'Rain'},
               {'FirstName': 'Mad', 'AId': 178520, 'LastName': 'Tain'}]

我希望找到例如Rai开头的'lastName'键列表中的位置

感谢您的帮助

2 个答案:

答案 0 :(得分:3)

rais = [i for i,d in enumerate(Player_list) if d['LastName'].startswith("Rai")]

输出:

>>> Player_list = [{'FirstName': 'Rad', 'AId': 178518, 'LastName': 'Nain'},
...                {'FirstName': 'Lad', 'AId': 178519, 'LastName': 'Rain'},
...                {'FirstName': 'Mad', 'AId': 178520, 'LastName': 'Tain'}]
>>> rais = [i for i,d in enumerate(Player_list) if d['LastName'].startswith("Rai")]
>>> rais
[1]

答案 1 :(得分:0)

或者:

[item["LastName"][:3] for item in player_list].index("Rai")