I am developing an add_to_index procedure that accepts 3 inputs:
- index: [[, [url1, url2, ...]], ...]
- keyword: String
- URL: String
If the keyword is already in the index, url is added to the list of URLs associated with this keyword.
If the keyword is not in the index, the new index refers to the index:
[keyword,[url]]
THE CODE
index = []
def add_to_index(index,keyword,url):
flag = 0
count = 0
for lists in index:
count += 1
if(lists[0]==keyword):
index[count][1].append(url)
if(flag ==0):
index.append([keyword,url])
add_to_index(index,'google','http://google.com')
print index
conclusion → [['google', 'http://google.com']]
add_to_index(index,'computing','http://acm.org')
print index
conclusion → [['google', 'http://google.com'], ['computing', 'http://acm.org']]
add_to_index(index,'google','http://gmail.com')
print index
error->
index[count][1].append(url)
AttributeError: 'str' object has no attribute 'append'
Expected Result:
[['google', ['http://google.com', 'http://gmail.com']],
['computing', ['http://acm.org']]]