简体   繁体   English

如何删除 python 字典中的键的一部分

[英]How do i remove part of a key in a python dictionary

I want to only see the times in the dictionary as it it currently being printed like我只想看到字典中的时间,因为它目前正在打印

字典

How do i only see, for example the 17:00 - 18:00 part of the dictionary.我怎么只看到,例如字典的 17:00 - 18:00 部分。

event_dates = {} #dictionary to store event dates and time
df = pd.read_html('https://www.dcu.ie/students/events')[0]['Event date']
event_dates.update(df)

I have tried using the pop method but doesn't seem to work.我试过使用 pop 方法,但似乎不起作用。 this is how i am printing it这就是我打印它的方式

for key in event_dates.values():
     print(key)

IIUC, you want to remove the dates and keep only the time. IIUC,您想删除日期并只保留时间。

You could use a regex:你可以使用正则表达式:

df = df.str.replace('([a-zA-Z]+ \d+, |\s+(?=\s))', '')

output: output:

0     09:00 - 17:00
1     12:45 - 14:00
2     17:00 - 18:00
3     13:00 - 14:00
4     13:15 - 13:35
...

You can do the following to print any desired key from the dictionary, just replace it with whatever time you need to print:您可以执行以下操作以从字典中打印任何所需的键,只需将其替换为您需要打印的任何时间即可:

    for key, value in event_dates.values():
         if key == event_dates["March 9, 17:00 - March 9, 18:00"]:
             print(value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM