How to add iteration values in dictionary? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to add iteration values in dictionary?

I want to make the dictionary, (key: days, value: occurence) in a textfile First, I have to extract lines which starts with 'From' and after split the line, In the line, extract the second word which is 'days' like monday, tuesday... And finally. Add the days as the key and occurences as the value in dictionary The expected result is.. {'Fri':20, 'Thu':6, 'Sat':1} I wrote as follow: file=open('mbox-short.txt') counts=dict() for line in file: line=line.rstrip() If line.startswith('From:'):continue If line.starswith('From'): words=line.split() days=words[2] for day in days: counts[day]=counts.get(day,0)+1 else: continue Print counts Is there anyone who can solve this problem? Help me out please-

6th Aug 2016, 5:21 PM
shin eun kyung
shin eun kyung - avatar
6 Answers
+ 1
Okay so here is the corrected code. Hopefully complies with Py2. Haven't used that in over a year. The readlines method is required as I mentioned earlier. Your code for counting the occurances was iterating the characters in the day string instead. My changes should be easy to understand. I hope. file=open('mbox-short.txt') counts=dict() lines = file.readlines() for line in lines: if line.startswith('From:'):continue if line.startswith('From'): words=line.split() day=words[2] if day in counts: counts[day] = counts[day]+1 else: counts[day] = 1 print counts
7th Aug 2016, 2:29 AM
Gershon Fosu
Gershon Fosu - avatar
+ 1
The IDLE editor tends to indent for me. 4 spaces for each indent. When I leave a construct I just backspace 4 times. There is an indent and dedent shortcut in IDLE which I sometimes use to indent and remove indentation in code blocks. Notepad++ has this shortcut too, though I don't know if it indents using tabs or 4 spaces. Select the code you want to indent and click tab. To dedent click shift tab.
7th Aug 2016, 9:57 AM
Gershon Fosu
Gershon Fosu - avatar
0
First, open needs an access mode. In your case the program needs to read the file. Add the access argument next to the file path. "r" for read. file = open(file, accessmode) Then use readlines to retrieve the lines in the file. lines = file.readlines() Then you can begin to iterate thru them. For the next part? The occurances, is it how many times a day appears in the file? Also can I see an example of a line in the file.
6th Aug 2016, 6:25 PM
Gershon Fosu
Gershon Fosu - avatar
0
Reading the file is no problem cuz default value is read mode. Accurrnce , means how many times the days are in the file :) File is like.. ******************* From eunkyung@uk.edu Sat Jan 089333 received (from Dr.Johnson) the name is... for the contents.. From:eunkyung@ac.kr 284000 Other commitment line is.. To impletment... From andrea@naver.kr Fri Jan 562388 How many appliances ... From matthew@google.com Fri Mar 48555 *********************************** Print counts #type is dictionary Output is.. {Fri:2 , Sat:1}
7th Aug 2016, 1:28 AM
shin eun kyung
shin eun kyung - avatar
0
You did really good job thanks! I got a result what I want But I have a question. Whenever I write programing on notepad++ connected with command prompt In notepad, it shows me where is indented lines occur with (-) sign. How to adjust indented line easily? Even I always check the column number on the bottom of the notepad , sometimes they make different indented lines.. I dont know how to figure out.. Can you let me know some tips for adjusting indented lines?
7th Aug 2016, 4:23 AM
shin eun kyung
shin eun kyung - avatar
0
Ok. Thank you for sharing your tip- I will imply it in that way :)
7th Aug 2016, 10:10 AM
shin eun kyung
shin eun kyung - avatar