有效地在词典列表中查找值

时间:2019-01-05 04:40:16

标签: python list dictionary lookup

我有一个字典列表

x = [{'name': 'attack', 'value': 10}, {'name': 'attack_side', 'value': 12}, ..., \
{'name': 'goals', 'value': 5}, ........]

例如,如何有效地确定列表中是否包含dic['name']'goals'的字典?

解决方案:

value = None
for dic in x:
        if dic['name'] == 'goals':
            value = int(dic['value'])
            break

列表x是足球运动员的统计信息。因此,没有进球的玩家将没有dic['name']'goals'的字典。

playerStats = [....]playerStats中的每个元素都具有类似的形式x,但是可能没有具有目标值的字典。

大约有800个查询


使用@ggorlen答案的实际解决方案

import numpy
import time

playerStats = numpy.load("/home/asus/Arief_tempo/others/Python/PL_data/players_stats.npy").item()
playerGoals = {}

a = time.perf_counter()
for name in playerStats.keys():
    lookup = {d['name']: d['value'] for d in playerStats[name]['stats']}
    try:        
        playerGoals[name] = int(lookup['goals'])
    except:
        playerGoals[name] = 0

print(time.perf_counter() - a)

这是playerStats中元素的实际示例,我上面提到的x可以与playerStats['Mohamed Salah']['stats']关联,'id'在这里将不使用。他有“进球”,但其他球员可能没有将“进球”作为关键。

playerStats['Mohamed Salah']
{'id': 5178.0, 'stats': [{'name': 'accurate_back_zone_pass', 'additionalInfo': {}, 'description': 'Todo: accurate_back_zone_pass', 'value': 70.0}, {'name': 'accurate_chipped_pass', 'additionalInfo': {}, 'description': 'Todo: accurate_chipped_pass', 'value': 10.0}, {'name': 'accurate_corners_intobox', 'additionalInfo': {}, 'description': 'Todo: accurate_corners_intobox', 'value': 6.0}, {'name': 'accurate_cross', 'additionalInfo': {}, 'description': 'Todo: accurate_cross', 'value': 11.0}, {'name': 'accurate_cross_nocorner', 'additionalInfo': {}, 'description': 'Todo: accurate_cross_nocorner', 'value': 5.0}, {'name': 'accurate_flick_on', 'additionalInfo': {}, 'description': 'Todo: accurate_flick_on', 'value': 5.0}, {'name': 'accurate_freekick_cross', 'additionalInfo': {}, 'description': 'Todo: accurate_freekick_cross', 'value': 0.0}, {'name': 'accurate_fwd_zone_pass', 'additionalInfo': {}, 'description': 'Todo: accurate_fwd_zone_pass', 'value': 377.0}, {'name': 'accurate_layoffs', 'additionalInfo': {}, 'description': 'Todo: accurate_layoffs', 'value': 19.0}, {'name': 'accurate_long_balls', 'additionalInfo': {}, 'description': 'Todo: accurate_long_balls', 'value': 7.0}, {'name': 'accurate_pass', 'additionalInfo': {}, 'description': 'Todo: accurate_pass', 'value': 436.0}, {'name': 'accurate_through_ball', 'additionalInfo': {}, 'description': 'Todo: accurate_through_ball', 'value': 8.0}, {'name': 'accurate_throws', 'additionalInfo': {}, 'description': 'Todo: accurate_throws', 'value': 3.0}, {'name': 'aerial_lost', 'additionalInfo': {}, 'description': 'Todo: aerial_lost', 'value': 32.0}, {'name': 'aerial_won', 'additionalInfo': {}, 'description': 'Todo: aerial_won', 'value': 9.0}, {'name': 'appearances', 'additionalInfo': {}, 'description': 'Todo: appearances', 'value': 21.0}, {'name': 'assist_penalty_won', 'additionalInfo': {}, 'description': 'Todo: assist_penalty_won', 'value': 1.0}, {'name': 'attempted_tackle_foul', 'additionalInfo': {}, 'description': 'Todo: attempted_tackle_foul', 'value': 5.0}, {'name': 'attempts_conceded_ibox', 'additionalInfo': {}, 'description': 'Todo: attempts_conceded_ibox', 'value': 95.0}, {'name': 'attempts_conceded_obox', 'additionalInfo': {}, 'description': 'Todo: attempts_conceded_obox', 'value': 61.0}, {'name': 'attempts_ibox', 'additionalInfo': {}, 'description': 'Todo: attempts_ibox', 'value': 50.0}, {'name': 'attempts_obox', 'additionalInfo': {}, 'description': 'Todo: attempts_obox', 'value': 19.0}, {'name': 'att_assist_openplay', 'additionalInfo': {}, 'description': 'Todo: att_assist_openplay', 'value': 34.0}, {'name': 'att_assist_setplay', 'additionalInfo': {}, 'description': 'Todo: att_assist_setplay', 'value': 3.0}, {'name': 'att_bx_centre', 'additionalInfo': {}, 'description': 'Todo: att_bx_centre', 'value': 23.0}, {'name': 'att_bx_left', 'additionalInfo': {}, 'description': 'Todo: att_bx_left', 'value': 2.0}, {'name': 'att_bx_right', 'additionalInfo': {}, 'description': 'Todo: att_bx_right', 'value': 10.0}, {'name': 'att_cmiss_high', 'additionalInfo': {}, 'description': 'Todo: att_cmiss_high', 'value': 2.0}, {'name': 'att_cmiss_left', 'additionalInfo': {}, 'description': 'Todo: att_cmiss_left', 'value': 2.0}, {'name': 'att_cmiss_right', 'additionalInfo': {}, 'description': 'Todo: att_cmiss_right', 'value': 3.0}, {'name': 'att_fastbreak', 'additionalInfo': {}, 'description': 'Todo: att_fastbreak', 'value': 6.0}, {'name': 'att_freekick_total', 'additionalInfo': {}, 'description': 'Todo: att_freekick_total', 'value': 1.0}, {'name': 'att_goal_high_centre', 'additionalInfo': {}, 'description': 'Todo: att_goal_high_centre', 'value': 1.0}, {'name': 'att_goal_high_left', 'additionalInfo': {}, 'description': 'Todo: att_goal_high_left', 'value': 1.0}, {'name': 'att_goal_low_centre', 'additionalInfo': {}, 'description': 'Todo: att_goal_low_centre', 'value': 4.0}, {'name': 'att_goal_low_left', 'additionalInfo': {}, 'description': 'Todo: att_goal_low_left', 'value': 5.0}, {'name': 'att_goal_low_right', 'additionalInfo': {}, 'description': 'Todo: att_goal_low_right', 'value': 2.0}, {'name': 'att_hd_target', 'additionalInfo': {}, 'description': 'Todo: att_hd_target', 'value': 3.0}, {'name': 'att_hd_total', 'additionalInfo': {}, 'description': 'Todo: att_hd_total', 'value': 3.0}, {'name': 'att_ibox_blocked', 'additionalInfo': {}, 'description': 'Todo: att_ibox_blocked', 'value': 7.0}, {'name': 'att_ibox_goal', 'additionalInfo': {}, 'description': 'Todo: att_ibox_goal', 'value': 13.0}, {'name': 'att_ibox_miss', 'additionalInfo': {}, 'description': 'Todo: att_ibox_miss', 'value': 12.0}, {'name': 'att_ibox_target', 'additionalInfo': {}, 'description': 'Todo: att_ibox_target', 'value': 18.0}, {'name': 'att_lf_goal', 'additionalInfo': {}, 'description': 'Todo: att_lf_goal', 'value': 11.0}, {'name': 'att_lf_target', 'additionalInfo': {}, 'description': 'Todo: att_lf_target', 'value': 11.0}, {'name': 'att_lf_total', 'additionalInfo': {}, 'description': 'Todo: att_lf_total', 'value': 56.0}, {'name': 'att_lg_centre', 'additionalInfo': {}, 'description': 'Todo: att_lg_centre', 'value': 1.0}, {'name': 'att_miss_high', 'additionalInfo': {}, 'description': 'Todo: att_miss_high', 'value': 4.0}, {'name': 'att_miss_high_left', 'additionalInfo': {}, 'description': 'Todo: att_miss_high_left', 'value': 3.0}, {'name': 'att_miss_high_right', 'additionalInfo': {}, 'description': 'Todo: att_miss_high_right', 'value': 1.0}, {'name': 'att_miss_left', 'additionalInfo': {}, 'description': 'Todo: att_miss_left', 'value': 4.0}, {'name': 'att_miss_right', 'additionalInfo': {}, 'description': 'Todo: att_miss_right', 'value': 6.0}, {'name': 'att_obox_blocked', 'additionalInfo': {}, 'description': 'Todo: att_obox_blocked', 'value': 10.0}, {'name': 'att_obox_miss', 'additionalInfo': {}, 'description': 'Todo: att_obox_miss', 'value': 6.0}, {'name': 'att_obox_target', 'additionalInfo': {}, 'description': 'Todo: att_obox_target', 'value': 3.0}, {'name': 'att_obx_centre', 'additionalInfo': {}, 'description': 'Todo: att_obx_centre', 'value': 16.0}, {'name': 'att_obx_right', 'additionalInfo': {}, 'description': 'Todo: att_obx_right', 'value': 2.0}, {'name': 'att_one_on_one', 'additionalInfo': {}, 'description': 'Todo: att_one_on_one', 'value': 2.0}, {'name': 'att_openplay', 'additionalInfo': {}, 'description': 'Todo: att_openplay', 'value': 58.0}, {'name': 'att_pen_goal', 'additionalInfo': {}, 'description': 'Todo: att_pen_goal', 'value': 2.0}, {'name': 'att_rf_goal', 'additionalInfo': {}, 'description': 'Todo: att_rf_goal', 'value': 2.0}, {'name': 'att_rf_target', 'additionalInfo': {}, 'description': 'Todo: att_rf_target', 'value': 7.0}, {'name': 'att_rf_total', 'additionalInfo': {}, 'description': 'Todo: att_rf_total', 'value': 10.0}, {'name': 'att_setpiece', 'additionalInfo': {}, 'description': 'Todo: att_setpiece', 'value': 3.0}, {'name': 'att_sv_high_centre', 'additionalInfo': {}, 'description': 'Todo: att_sv_high_centre', 'value': 1.0}, {'name': 'att_sv_high_left', 'additionalInfo': {}, 'description': 'Todo: att_sv_high_left', 'value': 1.0}, {'name': 'att_sv_low_centre', 'additionalInfo': {}, 'description': 'Todo: att_sv_low_centre', 'value': 8.0}, {'name': 'att_sv_low_left', 'additionalInfo': {}, 'description': 'Todo: att_sv_low_left', 'value': 5.0}, {'name': 'att_sv_low_right', 'additionalInfo': {}, 'description': 'Todo: att_sv_low_right', 'value': 6.0}, {'name': 'backward_pass', 'additionalInfo': {}, 'description': 'Todo: backward_pass', 'value': 151.0}, {'name': 'ball_recovery', 'additionalInfo': {}, 'description': 'Todo: ball_recovery', 'value': 61.0}, {'name': 'big_chance_created', 'additionalInfo': {}, 'description': 'Todo: big_chance_created', 'value': 13.0}, {'name': 'big_chance_missed', 'additionalInfo': {}, 'description': 'Todo: big_chance_missed', 'value': 11.0}, {'name': 'big_chance_scored', 'additionalInfo': {}, 'description': 'Todo: big_chance_scored', 'value': 8.0}, {'name': 'blocked_cross', 'additionalInfo': {}, 'description': 'Todo: blocked_cross', 'value': 2.0}, {'name': 'blocked_pass', 'additionalInfo': {}, 'description': 'Todo: blocked_pass', 'value': 25.0}, {'name': 'blocked_scoring_att', 'additionalInfo': {}, 'description': 'Todo: blocked_scoring_att', 'value': 17.0}, {'name': 'challenge_lost', 'additionalInfo': {}, 'description': 'Todo: challenge_lost', 'value': 5.0}, {'name': 'clean_sheet', 'additionalInfo': {}, 'description': 'Todo: clean_sheet', 'value': 9.0}, {'name': 'corner_taken', 'additionalInfo': {}, 'description': 'Todo: corner_taken', 'value': 23.0}, {'name': 'crosses_18yard', 'additionalInfo': {}, 'description': 'Todo: crosses_18yard', 'value': 8.0}, {'name': 'crosses_18yardplus', 'additionalInfo': {}, 'description': 'Todo: crosses_18yardplus', 'value': 4.0}, {'name': 'dispossessed', 'additionalInfo': {}, 'description': 'Todo: dispossessed', 'value': 72.0}, {'name': 'draws', 'additionalInfo': {}, 'description': 'Todo: draws', 'value': 3.0}, {'name': 'duel_lost', 'additionalInfo': {}, 'description': 'Todo: duel_lost', 'value': 143.0}, {'name': 'duel_won', 'additionalInfo': {}, 'description': 'Todo: duel_won', 'value': 80.0}, {'name': 'effective_blocked_cross', 'additionalInfo': {}, 'description': 'Todo: effective_blocked_cross', 'value': 2.0}, {'name': 'effective_clearance', 'additionalInfo': {}, 'description': 'Todo: effective_clearance', 'value': 2.0}, {'name': 'effective_head_clearance', 'additionalInfo': {}, 'description': 'Todo: effective_head_clearance', 'value': 1.0}, {'name': 'final_third_entries', 'additionalInfo': {}, 'description': 'Todo: final_third_entries', 'value': 36.0}, {'name': 'fouled_final_third', 'additionalInfo': {}, 'description': 'Todo: fouled_final_third', 'value': 12.0}, {'name': 'fouls', 'additionalInfo': {}, 'description': 'Todo: fouls', 'value': 15.0}, {'name': 'freekick_cross', 'additionalInfo': {}, 'description': 'Todo: freekick_cross', 'value': 4.0}, {'name': 'fwd_pass', 'additionalInfo': {}, 'description': 'Todo: fwd_pass', 'value': 114.0}, {'name': 'game_started', 'additionalInfo': {}, 'description': 'Todo: game_started', 'value': 20.0}, {'name': 'goals', 'additionalInfo': {}, 'description': 'Todo: goals', 'value': 13.0}, {'name': 'goals_conceded', 'additionalInfo': {}, 'description': 'Todo: goals_conceded', 'value': 9.0}, {'name': 'goals_conceded_ibox', 'additionalInfo': {}, 'description': 'Todo: goals_conceded_ibox', 'value': 9.0}, {'name': 'goals_openplay', 'additionalInfo': {}, 'description': 'Todo: goals_openplay', 'value': 9.0}, {'name': 'goal_assist', 'additionalInfo': {}, 'description': 'Todo: goal_assist', 'value': 7.0}, {'name': 'goal_assist_deadball', 'additionalInfo': {}, 'description': 'Todo: goal_assist_deadball', 'value': 1.0}, {'name': 'goal_assist_intentional', 'additionalInfo': {}, 'description': 'Todo: goal_assist_intentional', 'value': 7.0}, {'name': 'goal_assist_openplay', 'additionalInfo': {}, 'description': 'Todo: goal_assist_openplay', 'value': 5.0}, {'name': 'goal_assist_setplay', 'additionalInfo': {}, 'description': 'Todo: goal_assist_setplay', 'value': 2.0}, {'name': 'goal_fastbreak', 'additionalInfo': {}, 'description': 'Todo: goal_fastbreak', 'value': 1.0}, {'name': 'hand_ball', 'additionalInfo': {}, 'description': 'Todo: hand_ball', 'value': 1.0}, {'name': 'head_clearance', 'additionalInfo': {}, 'description': 'Todo: head_clearance', 'value': 1.0}, {'name': 'head_pass', 'additionalInfo': {}, 'description': 'Todo: head_pass', 'value': 25.0}, {'name': 'interception', 'additionalInfo': {}, 'description': 'Todo: interception', 'value': 4.0}, {'name': 'interception_won', 'additionalInfo': {}, 'description': 'Todo: interception_won', 'value': 4.0}, {'name': 'leftside_pass', 'additionalInfo': {}, 'description': 'Todo: leftside_pass', 'value': 216.0}, {'name': 'long_pass_own_to_opp', 'additionalInfo': {}, 'description': 'Todo: long_pass_own_to_opp', 'value': 13.0}, {'name': 'long_pass_own_to_opp_success', 'additionalInfo': {}, 'description': 'Todo: long_pass_own_to_opp_success', 'value': 10.0}, {'name': 'losses', 'additionalInfo': {}, 'description': 'Todo: losses', 'value': 1.0}, {'name': 'mins_played', 'additionalInfo': {}, 'description': 'Todo: mins_played', 'value': 1759.0}, {'name': 'offtarget_att_assist', 'additionalInfo': {}, 'description': 'Todo: offtarget_att_assist', 'value': 14.0}, {'name': 'ontarget_att_assist', 'additionalInfo': {}, 'description': 'Todo: ontarget_att_assist', 'value': 30.0}, {'name': 'ontarget_scoring_att', 'additionalInfo': {}, 'description': 'Todo: ontarget_scoring_att', 'value': 34.0}, {'name': 'open_play_pass', 'additionalInfo': {}, 'description': 'Todo: open_play_pass', 'value': 556.0}, {'name': 'overrun', 'additionalInfo': {}, 'description': 'Todo: overrun', 'value': 7.0}, {'name': 'passes_left', 'additionalInfo': {}, 'description': 'Todo: passes_left', 'value': 47.0}, {'name': 'passes_right', 'additionalInfo': {}, 'description': 'Todo: passes_right', 'value': 119.0}, {'name': 'penalty_won', 'additionalInfo': {}, 'description': 'Todo: penalty_won', 'value': 3.0}, {'name': 'pen_area_entries', 'additionalInfo': {}, 'description': 'Todo: pen_area_entries', 'value': 93.0}, {'name': 'poss_lost_all', 'additionalInfo': {}, 'description': 'Todo: poss_lost_all', 'value': 323.0}, {'name': 'poss_lost_ctrl', 'additionalInfo': {}, 'description': 'Todo: poss_lost_ctrl', 'value': 323.0}, {'name': 'poss_won_att_3rd', 'additionalInfo': {}, 'description': 'Todo: poss_won_att_3rd', 'value': 20.0}, {'name': 'poss_won_def_3rd', 'additionalInfo': {}, 'description': 'Todo: poss_won_def_3rd', 'value': 14.0}, {'name': 'poss_won_mid_3rd', 'additionalInfo': {}, 'description': 'Todo: poss_won_mid_3rd', 'value': 27.0}, {'name': 'put_through', 'additionalInfo': {}, 'description': 'Todo: put_through', 'value': 25.0}, {'name': 'rightside_pass', 'additionalInfo': {}, 'description': 'Todo: rightside_pass', 'value': 92.0}, {'name': 'shot_fastbreak', 'additionalInfo': {}, 'description': 'Todo: shot_fastbreak', 'value': 6.0}, {'name': 'shot_off_target', 'additionalInfo': {}, 'description': 'Todo: shot_off_target', 'value': 18.0}, {'name': 'successful_final_third_passes', 'additionalInfo': {}, 'description': 'Todo: successful_final_third_passes', 'value': 261.0}, {'name': 'successful_open_play_pass', 'additionalInfo': {}, 'description': 'Todo: successful_open_play_pass', 'value': 419.0}, {'name': 'successful_put_through', 'additionalInfo': {}, 'description': 'Todo: successful_put_through', 'value': 6.0}, {'name': 'total_att_assist', 'additionalInfo': {}, 'description': 'Todo: total_att_assist', 'value': 44.0}, {'name': 'total_back_zone_pass', 'additionalInfo': {}, 'description': 'Todo: total_back_zone_pass', 'value': 82.0}, {'name': 'total_chipped_pass', 'additionalInfo': {}, 'description': 'Todo: total_chipped_pass', 'value': 30.0}, {'name': 'total_clearance', 'additionalInfo': {}, 'description': 'Todo: total_clearance', 'value': 2.0}, {'name': 'total_contest', 'additionalInfo': {}, 'description': 'Todo: total_contest', 'value': 61.0}, {'name': 'total_corners_intobox', 'additionalInfo': {}, 'description': 'Todo: total_corners_intobox', 'value': 20.0}, {'name': 'total_cross', 'additionalInfo': {}, 'description': 'Todo: total_cross', 'value': 36.0}, {'name': 'total_cross_nocorner', 'additionalInfo': {}, 'description': 'Todo: total_cross_nocorner', 'value': 16.0}, {'name': 'total_distance_in_m', 'additionalInfo': {}, 'description': 'Todo: total_distance_in_m', 'value': 10282.4443359375}, {'name': 'total_fastbreak', 'additionalInfo': {}, 'description': 'Todo: total_fastbreak', 'value': 9.0}, {'name': 'total_final_third_passes', 'additionalInfo': {}, 'description': 'Todo: total_final_third_passes', 'value': 363.0}, {'name': 'total_flick_on', 'additionalInfo': {}, 'description': 'Todo: total_flick_on', 'value': 13.0}, {'name': 'total_fwd_zone_pass', 'additionalInfo': {}, 'description': 'Todo: total_fwd_zone_pass', 'value': 527.0}, {'name': 'total_launches', 'additionalInfo': {}, 'description': 'Todo: total_launches', 'value': 2.0}, {'name': 'total_layoffs', 'additionalInfo': {}, 'description': 'Todo: total_layoffs', 'value': 23.0}, {'name': 'total_long_balls', 'additionalInfo': {}, 'description': 'Todo: total_long_balls', 'value': 15.0}, {'name': 'total_offside', 'additionalInfo': {}, 'description': 'Todo: total_offside', 'value': 18.0}, {'name': 'total_pass', 'additionalInfo': {}, 'description': 'Todo: total_pass', 'value': 573.0}, {'name': 'total_pull_back', 'additionalInfo': {}, 'description': 'Todo: total_pull_back', 'value': 1.0}, {'name': 'total_scoring_att', 'additionalInfo': {}, 'description': 'Todo: total_scoring_att', 'value': 69.0}, {'name': 'total_sub_off', 'additionalInfo': {}, 'description': 'Todo: total_sub_off', 'value': 6.0}, {'name': 'total_sub_on', 'additionalInfo': {}, 'description': 'Todo: total_sub_on', 'value': 1.0}, {'name': 'total_tackle', 'additionalInfo': {}, 'description': 'Todo: total_tackle', 'value': 10.0}, {'name': 'total_through_ball', 'additionalInfo': {}, 'description': 'Todo: total_through_ball', 'value': 11.0}, {'name': 'total_throws', 'additionalInfo': {}, 'description': 'Todo: total_throws', 'value': 5.0}, {'name': 'touches', 'additionalInfo': {}, 'description': 'Todo: touches', 'value': 957.0}, {'name': 'touches_in_opp_box', 'additionalInfo': {}, 'description': 'Todo: touches_in_opp_box', 'value': 165.0}, {'name': 'turnover', 'additionalInfo': {}, 'description': 'Todo: turnover', 'value': 59.0}, {'name': 'unsuccessful_touch', 'additionalInfo': {}, 'description': 'Todo: unsuccessful_touch', 'value': 59.0}, {'name': 'was_fouled', 'additionalInfo': {}, 'description': 'Todo: was_fouled', 'value': 21.0}, {'name': 'wins', 'additionalInfo': {}, 'description': 'Todo: wins', 'value': 17.0}, {'name': 'won_contest', 'additionalInfo': {}, 'description': 'Todo: won_contest', 'value': 41.0}, {'name': 'won_corners', 'additionalInfo': {}, 'description': 'Todo: won_corners', 'value': 23.0}, {'name': 'won_tackle', 'additionalInfo': {}, 'description': 'Todo: won_tackle', 'value': 5.0}]}

3 个答案:

答案 0 :(得分:4)

词典字典提供O(1)查找时间:

x = [
    {'name': 'attack', 'value': 10}, 
    {'name': 'attack_side', 'value': 12},
    {'name': 'goals', 'value': 5}
]

lookup = {d['name']: d for d in x}

print(lookup['goals'])

输出:

{'name': 'goals', 'value': 5}

Try it!

注意:

  • 如果您只需要检索lookup = {d['name']: d['value'] for d in x}键而不是整个字典,则使用value
  • 构建一次字典,然后将其用于所有查找。
  • 如果仅执行一次查找,则您已经拥有了最佳解决方案。
  • 不能有重复的name键。

答案 1 :(得分:3)

尝试类似的东西:

代码:

return db.collection('projects').aggregate([
  {
    $match: {
      agents: ObjectId(agent)
    }
  },
  {
    $lookup: {
      from: "agents",
      localField: "agents",
      foreignField: "_id",
      as: "agents"
    }
  },
  {
    $lookup: {
      from: "agencies",
      localField: "agency",
      foreignField: "_id",
      as: "agency"
    }
  },      
  {
    $lookup: {
      from: "roles",
      let: { "roles": "$roles" },
      pipeline: [
        { $match: { $expr: { $in: [ "$_id", "$$roles" ] } } },
        {
          $lookup: {
            from: "resources",
            let: { "applicants": "$applicants" },
            pipeline: [
              { $match: { $expr: { $in: [ "$_id", "$$applicants" ] } } },
              {
                $project: {
                  first_name: 1
                }
              }
            ],
            as: "applicants"
          }
        }
      ],
      as: "roles"
    }
  }
])

这使用生成器表达式浏览列表,并返回第一个匹配的实例;如果没有匹配的实例,则返回goals = next((d for d in data if d['name'] == 'goals'), None)

测试代码:

None

结果:

data = [
    {'name': 'attack', 'value': 10},
    {'name': 'attack_side', 'value': 12},
    {'name': 'goals', 'value': 5}
]

goals = next((d for d in data if d['name'] == 'goals'), None)
print(goals)

no_goals = next((d for d in data if d['name'] == 'no_goals'), None)
print(no_goals)

答案 2 :(得分:0)

我建议采用另一种方法:将字典列表以另一种格式转换为精确的dict:

x = [{'name': 'attack', 'value': 10}, {'name': 'attack_side', 'value': 12},{'name': 'goals', 'value': 5}]

player_x = { ability['name']: ability['value'] for ability in x }
#=> {'attack': 10, 'attack_side': 12, 'goals': 5}

然后很容易获得:

print(player_x.get('attack', None)) #=> 10
print(player_x.get('whathever', None)) #=> None

或者:

for ability in player_x.keys():
  print(ability, player_x[ability])

# attack 10
# attack_side 12
# goals 5