Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary Basics
#1
Good Day,

I'd like to load certain data concerning HOLIDAYS into a python dictionary. Later downstream in my business process I then want to take an input date 'look it up' in that dictionary to make use of other dictionary elements which tell me to add or subtract a day from that input date and print it out/use it elsewhere for finding information about that 'adjusted' date. For this post, all I want to do, is read in my .csv and create a dictionary 'row' for each row in the .csv.

The dictionary has this format roughly in pseudo-code: {Datetocompareinputto:Date in string format like '20181225',
Day of week of that date: string like 'Monday', 'Tuesday' etc.
Description: like 'Christmas 2018'
Days to add: Integer 1,2,3
Days to subtract: Negative Integer -1,-2,-3
Here is my code:

#Open file, load dictionary, then close file
reader=csv.reader(Holiday_File)
Holiday_DICT={'Holiday':1/1/1900,'Dayofwk':"Sunday",'Desc':"dummy",'Daystoadd':1,'Daystosubtract':-1}
for row in reader:
    #print(row[0],row[1],row[2],row[3],row[4])
    item=row[0]
    Holiday_DICT[item]=row[0],row[1],row[2],row[3],row[4]
Holiday_File.close()
I get results like this when I print(Holiday_DICT):
{'Holiday': 0.0005263157894736842, 'Dayofwk': 'Sunday', 'Desc': 'dummy', 'Daystoadd': 1, 'Daystosubtract': -1, '20160101': ('20160101', 'Friday', "New Year's Day", '1', '-1'), '20160118': ('20160118', 'Monday', 'Martin Luther King Jr. Day', '1', '-1')more....}

I have looked at various posts online and videos, but am chasing my tail around. With a LIST I can use .append method, but with dictionary all I see is vague rumblings that [ITEM] might mean something in referencing the 'THING' I want to hook on to to simply ADD ENTRIES to my dictionary. I get some results but feel like, nah man this is too hard and just not right yet. In sum I just want my Date String to be the lookup value that is unique, BUT it must also have in that same dictionary entry, the other 4 elements tied to it. I would prefer to have each element, be an independent 'item' within each dictionary entry keyed by that date. What is best practice to simply add my .csv rows to my dictionary in that manner, row by row??

Thank you for your help.
Reply
#2
Here's the thing: Your holiday dict is set up to store ONE holiday. So you either need a list of similar dicts, or a dict of similar dicts. If you want to look up an input date, then you should have a dict of dicts, with the key being the date. That is basically what your for loop is doing, but that's not how you initialize it. Try running your code but start with a blank dictionary on line 3.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Hi and thanks for the input. So, I reverted back to where I was to begin this post and took your suggestion to initialize with an empty dictionary as follows(please ignore all the comments):

reader=csv.reader(Holiday_File)
Holiday_DICT={}
#Holiday_DICT={'Holiday':1/1/1900,'Dayofwk':"Sunday",'Desc':"dummy",'Daystoadd':1,'Daystosubtract':-1}
#Holiday_DICT={'Holiday':1/1/1900,(['Dayofwk','Desc','Daystoadd','Daystosubtract'])}
x=0
for row in reader:
    #print(row[0],row[1],row[2],row[3],row[4])
    item=row[0]
    #x=0
    #Holiday_DICT[item]=row[0],row[1],row[2],row[3],row[4]
    #Holiday=row[0]
    #Dayofwk=row[1]
    #Desc=row[2]
    #Daystoadd=row[3]
    #Daystosubtract=row[4]
    Holiday_DICT[item]=row[0],row[1],row[2],row[3],row[4]
    #Holiday_DICT[Holiday]=Holiday
    #Holiday_DICT[Dayofwk]=Dayofwk
    #Holiday_DICT[Desc]=Desc
    #Holiday_DICT[Daystoadd]=Daystoadd
    #Holiday_DICT[Daystosubtract]=Daystosubtract
    #x=x+row
    #print("x: ", x)
Holiday_File.close()
Here are the results:
{'20160101': ('20160101', 'Friday', "New Year's Day", '1', '-1'), '20160118': ('20160118', 'Monday', 'Martin Luther King Jr. Day', '1', '-1'), '20160216': ('20160216', 'Monday', "Washington's Birthday", '1', '-1'), '20160325': ('20160325', 'Friday', 'Good Friday', '2', '-1'), '20160530': ('20160530', 'Monday', 'Memorial Day', '1', '-1'), '20160704': ('20160704', 'Monday', 'Independence Day', '1', '-1'), '20160905': ('20160905', 'Monday', 'Labor Day', '1', '-1'), '20161124': ('20161124', 'Thursday', 'Thanksgiving Day', '1', '-2'), '20161225': ('20161225', 'Sunday', 'Christmas Day', '2', '-1'), '20170102': ('20170102', 'Monday', '1- JAN (Observed Monday, January 2)', '1', '-1'), '20170116': ('20170116', 'Monday', 'Monday, January 16', '1', '-1'), '20170220': ('20170220', 'Monday', 'Monday, February 20', '1', '-1'), '20170414': ('20170414', 'Friday', 'Friday, April 14', '1', '-1'), '20170529': ('20170529', 'Monday', 'Monday, May 29', '2', '-2'), '20170704': ('20170704', 'Tuesday', 'Tuesday, July 4*', '1', '-1'), '20170904': ('20170904', 'Monday', 'Monday, September 4', '1', '-1'), '20171123': ('20171123', 'Thursday', 'Thursday, November 23**', '1', '-1'), '20171225': ('20171225', 'Monday', 'Monday, December 25', '1', '-1'), '20180101': ('20180101', 'Monday', 'New Years Day', '1', '-1'), '20180115': ('20180115', 'Monday', 'Martin Luther King, Jr. Day', '1', '-1'), '20180219': ('20180219', 'Monday', "Washington's Birthday", '1', '-1'), '20180330': ('20180330', 'Friday', 'Good Friday', '1', '-1')....more}

This looks pretty good, but I guess I'd like to know what has actually been created. I have a KEY of that date referenced above, then the other attributes lumped together within () which looks like a LIST as maybe the VALUE of that date KEY??? Is that right?? I don't think it's created a DICT within a DICT or has it? Just trying to learn what's what even though I think I could make these results work, one way or another.

Again thanks.
Reply
#4
Oh, whoops. That is actually a tuple as the value of a key. A tuple is like a list, but can't be changed in place like a list can. To get a dictionary as the value, you can do:

Holiday_DICT[item] = {'Holiday': row[0], 'Dayofwk': row[1], 'Desc': row[2], 'Daystoadd': row[3],
    'Daystosubtract': row[4]}
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Why not use csv.DictReader ?
reader = csv.DictReader(
    Holiday_File,
    ["Holiday", "Dayofwk", "Desc", "Daystoadd", "Daystosubstract"])

Holiday_DICT = {}

for row in reader:
    Holiday_DICT[row["Holiday"]] = row
Reply
#6
Because I had NO IDEA such a method existed!! :) :) Thank you for that, I'll check that out also. Looks like I might have several options here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Learned Python Basics, what now? muzikman 20 5,857 Oct-11-2021, 01:34 AM
Last Post: Underscore
  PYserial basics? bako 10 5,023 Apr-26-2020, 09:15 PM
Last Post: bowlofred
  [split] Debugging scripts basics tultalk 5 3,018 Apr-25-2020, 01:02 AM
Last Post: menator01
  Debugging scripts basics bako 8 3,420 Apr-24-2020, 06:18 AM
Last Post: Larz60+
  Problem with basics Wraith2102 2 1,962 Jun-17-2019, 06:51 PM
Last Post: perfringo
  What to do after learning python basics xyzabc12310000 1 2,817 May-20-2018, 11:43 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020