嗨,我有一个dict列表
Player_list = [{'FirstName': 'Rad', 'AId': 178518, 'LastName': 'Nain'},
{'FirstName': 'Lad', 'AId': 178519, 'LastName': 'Rain'},
{'FirstName': 'Mad', 'AId': 178520, 'LastName': 'Tain'}]
我希望找到例如Rai开头的'lastName'键列表中的位置
感谢您的帮助
答案 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")