简体   繁体   中英

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.

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. 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.

You could use a regex:

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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