python词典列表分组和过滤问题

时间:2016-07-25 08:00:52

标签: python dictionary group-by

我有以下词典列表。该列表已经排序。现在我必须按工人分组并获得他的“prskill”,但如果没有其他选择,他将获得无。

对于每个工人,最多有两个字典,一个带有“prskill”无,另一个带有实际值。如果只有一个字典他的“prskill”是无

我的清单

sorted =    [{worker_nick:  1B  prskill:  None },
{worker_nick:  B1  prskill:  None },
{worker_nick:  B2  prskill:  None },
{worker_nick:  Božić  prskill:  None },
{worker_nick:  Božić  prskill:  Bolničar },
{worker_nick:  Cimermančič  prskill:  None },
{worker_nick:  Cimermančič  prskill:  Bolničar },
{worker_nick:  CindrićJ  prskill:  None },
{worker_nick:  CindrićJ  prskill:  razno },
{worker_nick:  CipurićA  prskill:  None },
{worker_nick:  CipurićA  prskill:  Strežnik },
{worker_nick:  Dančulović  prskill:  None },
{worker_nick:  Dančulović  prskill:  Strežnik },
{worker_nick:  Dragovan  prskill:  Bolničar },
{worker_nick:  Dragovan  prskill:  None },
{worker_nick:  Fofana  prskill:  SestraOdd },
{worker_nick:  Fofana  prskill:  None },
{worker_nick:  GovednikM  prskill:  None },
{worker_nick:  GovednikM  prskill:  Strežnik },
{worker_nick:  Hoenigman  prskill:  None },
{worker_nick:  Hoenigman  prskill:  SestraOdd },
{worker_nick:  Husič  prskill:  None },
{worker_nick:  Huskić  prskill:  Bolničar },
{worker_nick:  Huskić  prskill:  None },
{worker_nick:  JD-Šuligoj  prskill:  JD },
{worker_nick:  JD-Šuligoj  prskill:  None },
{worker_nick:  Jakša  prskill:  Gospodinja },
{worker_nick:  Jakša  prskill:  None },
{worker_nick:  Kastelic  prskill:  SestraOdd },
{worker_nick:  Kastelic  prskill:  None },
{worker_nick:  Lukinić  prskill:  SestraOdd },
{worker_nick:  Lukinić  prskill:  None },
{worker_nick:  MaceleJ  prskill:  None },
{worker_nick:  MaceleJ  prskill:  Bolničar },
{worker_nick:  MaceleM  prskill:  SestraAmb },
{worker_nick:  MaceleM  prskill:  None },
{worker_nick:  Miketič  prskill:  Bolničar },
{worker_nick:  Miketič  prskill:  None },
{worker_nick:  MikešićG  prskill:  SestraOdd },
{worker_nick:  MikešićG  prskill:  None },
{worker_nick:  Muc  prskill:  None },
{worker_nick:  Muc  prskill:  Bolničar },
{worker_nick:  Petraš  prskill:  None },
{worker_nick:  Petraš  prskill:  Terapevt },
{worker_nick:  Pezdirc  prskill:  SestraOdd },
{worker_nick:  Pezdirc  prskill:  None },
{worker_nick:  Prevalšek  prskill:  Bolničar },
{worker_nick:  Prevalšek  prskill:  None },
{worker_nick:  Ramušćak  prskill:  SestraAmb },
{worker_nick:  Ramušćak  prskill:  None },
{worker_nick:  S-T1  prskill:  None },
{worker_nick:  S-T2  prskill:  None },
{worker_nick:  S1  prskill:  None },
{worker_nick:  Slanc  prskill:  Terapevt },
{worker_nick:  Slanc  prskill:  None },
{worker_nick:  Sneljer  prskill:  Terapevt },
{worker_nick:  Sneljer  prskill:  None },
{worker_nick:  Stepan  prskill:  SestraOdd },
{worker_nick:  Stepan  prskill:  None },
{worker_nick:  Sudac  prskill:  None },
{worker_nick:  Sudac  prskill:  Bolničar },
{worker_nick:  Tkalac  prskill:  Bolničar },
{worker_nick:  Tkalac  prskill:  None },
{worker_nick:  Vidovič  prskill:  SestraOdd },
{worker_nick:  Vidovič  prskill:  None },
{worker_nick:  VukšiničM  prskill:  None },
{worker_nick:  VukšiničM  prskill:  Bolničar },
{worker_nick:  Vučič  prskill:  Bolničar },
{worker_nick:  Vučič  prskill:  None },
{worker_nick:  Čurči  prskill:  None },
{worker_nick:  Čurči  prskill:  Bolničar },
{worker_nick:  Šterk  prskill:  None },
{worker_nick:  Šterk  prskill:  Namestnik direktorja }]

有什么建议吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码: -

filteredResult = {}
for sortedDict in sorted:
  if sortedDict['worker_nick'] in filteredResult:
    if None is not sortedDict['prskill']:
      filteredResult[sortedDict['worker_nick']] = sortedDict
  else:
    filteredResult[sortedDict['worker_nick']] = sortedDict

filteredResult.values()

这是没有顺序的O / P,如果你想要它有序,那么你必须使用orderedDict而不是简单的dict。