Python - 如何在词典中达到某个值 [英] Python -how to reach a certain value in a dictionnary

查看:68
本文介绍了Python - 如何在词典中达到某个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的词典:

({'Musique':[['Musique','Shawn Phillips',236,236],['Musique ',L'avenueRoyalefêtel'été!,237,237],['Musique','Perséidesmusicales',215,215],['Musique','GaétanLeclercchanteFélixet......',229, 229],['Musique','火环:Johnny Cash Experience',202,202],['Musique',Jazz'Art,208,210],{'Théatre':[['Théâtre ','Coupdethéâtreàlajoyeusemaisonhantée',189,189],['Théâtre','Les galoches du bonheur',203,203],['Théâtre','Le voyage de Pixelle dans le monde virtuel' ,217,217],['Théâtre','Marimbaàlaferme de la bonne entente',224,224],['Théâtre','Pattes et cravates',196,196],{'Danse':[[ 'Danse','Initiation au tango argentin suivi de la milonga',182,231],['Danse','Samedi de danser ......',188,188],['Danse','Rusdell Nunez(拉丁裔) ',191, 191] 







这个词典有3种类型:'Musique','Théâtre',' Danse'



我的dictionnary中的所有int都是这些事件可用的时候。我需要返回一个列表,其中包含在参数 day_year


以下是完整的说明和功能:



 def obtain_events_date_type(dictio_events_par_type,day_year,type_event):







为此,该功能首先过滤

diction_events_par_type。

然后,对于正确类型的每个事件作为参数 type_event ,如果事件的开头(这是字典中的第一个int较低或者即使在int,如果事件的结尾(你可以说的每个索引的最后一个)高于或甚至比参数 day_year ,我们可以添加名称这个事件发生在今天可用的事件列表中。我需要返回事件列表。



所以,如果我输入 

< pre> def obtain_events_date_type(dictio_events_pa r_type,236,'Musique'):





我需要添加236天可用的所有事件,例如首先就像我在这篇文章中提到的词典一样。可能在同一天有超过1个活动。







为简化起见事情,只是使用我给你的dictio所以你不必过滤第一个参数,我只需要能够在我的dictionnary中定位每个int,所以我可以将它们添加到我的列表中,但它似乎工作就像一个列表,因为它的字典和我很难。



我尝试过:



尝试使用像列表一样的索引作为例子,如果我想为每年的一天中的每一个索引[2]和索引[3]定位,但是它不会让我因为它是一个词典。

解决方案

[更新]

 dict = {  Musique':[['  Musique''  Shawn Phillips' 236  236 ],['  Musique'  L'avenueRoyalefêtel' été! 237  237 ],['  Musique'' Perséides musicales' 215  215 ],['  Musique'' GaétanLeclerc chanteFélixet......' 229  229 ],['  Musique'' 火环:约翰尼加sh经验' 202  202 ],['  Musique'  Jazz 'Art 208  210 ]],' Théatre':[[' Théâtre''  Coupdethéâtreàlajoyeusemaisonhantée' 189  189 ],[' Théâtre''  Les galoches du bonheur' 203  203 ],[' 剧院 '  Le voyage de Pixelle dans le monde virtuel' 217  217 ],[' Théâtre'' Marimbaàlaferme de la bonne entente',< span class =code-digit> 224 , 224 ],[' Théâtre''  Pattes et cravates',< span class =code-digit> 196 , 196 ]],'  Danse':[['  Danse',< span class =code-string>'  Initiation au tango argentin suivi de la milonga' 1 82  231 ],['  Danse ''  Samedi de danser ...' 188  188 ],['  Danse''  Rusdell Nunez(拉丁裔)' 191  191 ]]} 


def obtain_events_date_type(dictio_events_par_type,day_year,type_event):
keys = list(dictio_events_par_type.keys())
sellist = dict [type_event]
avail = [x x sellist if day_year> = x [< span class =code-digit> 2 ] day_year< = x [ 3 ]]
return avail

print (obtain_events_date_type(dict, 236 ' 音乐节'))

<小> [/更新]


I have a dictionnary like this one:

({'Musique': [['Musique', 'Shawn Phillips', 236, 236], ['Musique', "L'avenue Royale fête l'été!", 237, 237], ['Musique', 'Perséides musicales', 215, 215], ['Musique', 'Gaétan Leclerc chante Félix et…', 229, 229], ['Musique', 'The Ring of Fire : a Johnny Cash Experience', 202, 202], ['Musique', "Jazz'Art", 208, 210],  {'Théatre': [['Théâtre', 'Coup de théâtre à la joyeuse maison hantée', 189, 189], ['Théâtre', 'Les galoches du bonheur', 203, 203], ['Théâtre', 'Le voyage de Pixelle dans le monde virtuel', 217, 217], ['Théâtre', 'Marimba à la ferme de la bonne entente', 224, 224], ['Théâtre', 'Pattes et cravates', 196, 196], {'Danse': [['Danse', 'Initiation au tango argentin suivi de la milonga', 182, 231], ['Danse', 'Samedi de danser...', 188, 188], ['Danse', 'Rusdell Nunez (latino)', 191, 191] 




There are 3 types in this dictionnary : 'Musique','Théâtre','Danse'

All the int in my dictionnary are when those events are available. I need to return a list with the name of all the events with the right type who are offered at the date entereted in argument day_year

Here is the full instructions and the function:

def obtain_events_date_type(dictio_events_par_type, day_year, type_event):




To do this, the function starts by filtering
diction_events_par_type.
Then, for each event of the right type as the argument type_event, if the beginning of the event(which is the first int in the dictionnary is lower or even at the int and if the end of the event(the last in of every index you could say) is higher or even than the argument day_year, we can add the name of this event in the list of event since its available on this day. i need to return that list of events.

So if i entered  

<pre>def obtain_events_date_type(dictio_events_par_type, 236, 'Musique'):



I would need to add all events that are available on the day 236, for exemple the first like in my dictionnary mentionned in this post. Its possible there is more than 1 events on the same day.



For the sake of simplifying things, just use the dictio i gave you so you dont have to filter the first argument, i just need to be able to target each int in my dictionnary so i can add them to my list, but it doenst seem to be working like a list since its a dictionary and im having a hard time.

What I have tried:

Tried to use index like a list for exemple if i want to target every index[2] and index[3] for my day of the year who are int, but it wouldnt let me since its a dictionnary.

解决方案

[Update]

dict = {'Musique': [['Musique', 'Shawn Phillips', 236, 236], ['Musique', "L'avenue Royale fête l'été!", 237, 237], ['Musique', 'Perséides musicales', 215, 215], ['Musique', 'Gaétan Leclerc chante Félix et…', 229, 229], ['Musique', 'The Ring of Fire : a Johnny Cash Experience', 202, 202], ['Musique', "Jazz'Art", 208, 210]],  'Théatre': [['Théâtre', 'Coup de théâtre à la joyeuse maison hantée', 189, 189], ['Théâtre', 'Les galoches du bonheur', 203, 203], ['Théâtre', 'Le voyage de Pixelle dans le monde virtuel', 217, 217], ['Théâtre', 'Marimba à la ferme de la bonne entente', 224, 224], ['Théâtre', 'Pattes et cravates', 196, 196]], 'Danse': [['Danse', 'Initiation au tango argentin suivi de la milonga', 182, 231], ['Danse', 'Samedi de danser...', 188, 188], ['Danse', 'Rusdell Nunez (latino)', 191, 191]]}


def obtain_events_date_type(dictio_events_par_type, day_year, type_event):
  keys = list(dictio_events_par_type.keys())
  sellist = dict[type_event]
  avail = [x for x in sellist if day_year>=x[2] and day_year<=x[3] ]
  return avail

print( obtain_events_date_type(dict, 236, 'Musique'))

[/update]


这篇关于Python - 如何在词典中达到某个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆