Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python(2.7) Question Please use setdefault(). Example codes are below but dont u

ID: 666167 • Letter: P

Question

Python(2.7) Question

Please use setdefault(). Example codes are below but dont use the same codes, change it. Kind of paraphrase the codes

example codes:

import recommendations
opened = open('ydata-delicious-popular-urls-and-tags-v1_0.txt')
dict = {}
counter=0

for line in opened:
    counter+=1
    if counter==100:
        break

    list = line.split(' ')
    list.pop(1)
    list.pop(1)
    url=list[0]
    dict.setdefault(url,{})
    list.pop(0)
    if len(list)==1:
        continue
    for i in range (0,len(list),2):
        new=list[i:i+2]
        #url=new[0],int(new[1])
        dict[url].setdefault(new[0],int(new[1]))

print dict

a little part of output:

{'http://del.icio.us/joshua': {'joshua': 43, 'web': 18, 'links': 37, 'tags': 18, 'blogs': 8, 'people': 36, 'blog': 7, 'del.icio.us': 111, 'Delicious': 71, 'bookmarks': 8}

data: https://drive.google.com/file/d/0Bz_aB61VIRZAa0M5dnlOQTdST3M/view?usp=sharing

Explanation / Answer

import recommendations
opened = open('ydata-delicious-popular-urls-and-tags-v1_0.txt')
dict = {}
counter=0

for line in opened:
    counter+=1
    if counter==100:
        break

    list = line.split(' ')
    list.pop(1)
    list.pop(1)
    url=list[0]
    dict.setdefault(url,{})
    list.pop(0)
    if len(list)==1:
        continue
    for i in range (0,len(list),2):
        new=list[i:i+2]
        #url=new[0],int(new[1])
        dict[url].setdefault(new[0],int(new[1]))

print dict