5

I am developing a procedure add_to_index, that takes 3 inputs:

  • an index: [[,[url1,url2,...]],...]
  • a keyword: String
  • a url: String

If the keyword is already in the index, url is added to the list of urls associated with that keyword.

If the keyword is not in the index, a new element is to the index:

[keyword,[url]]

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])   

#calling the function below

add_to_index(index,'google','http://google.com')
print index

output -> [['google', 'http://google.com']]

add_to_index(index,'computing','http://acm.org')
print index

output -> [['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 output:

 [['google', ['http://google.com', 'http://gmail.com']], 
 ['computing', ['http://acm.org']]]
2
  • 1
    It really seems like this is the wrong data structure for this: you'd be much better off using a dictionary and a dictbuilder. Commented Dec 15, 2014 at 20:11
  • 1
    It was from a quiz that I got at a competitive programming competition. Commented Dec 15, 2014 at 20:12

5 Answers 5

3

You have done three mistakes, Firstly you have not used the flag and secondly you are adding the url as a string. And finally as Kaivosuketaja has mentioned in the comment, count should be incremented in the end. It can be done otherwise as

index = []

def add_to_index(index,keyword,url):
    flag = 0
    count = 0

    for lists in index:

        if(lists[0]==keyword): 
            flag = 1
            index[count][1].append(url)
        count += 1

    if(flag ==0):
        index.append([keyword,[url]])   
        # Take note of append away here
#calling the function below

add_to_index(index,'google','http://google.com')
print index

add_to_index(index,'computing','http://acm.org')
print index

add_to_index(index,'google','http://gmail.com') 
print index

The output now is

[['google', ['http://google.com']]]
[['google', ['http://google.com']], ['computing', ['http://acm.org']]]
[['google', ['http://google.com', 'http://gmail.com']], ['computing', ['http://acm.org']]]
Sign up to request clarification or add additional context in comments.

2 Comments

Make that three mistakes - count should be incremented at the end of the for loop.
@Kaivosukeltaja I'm sorry if you feel bad, But I took three mins to write your name as I couldn't copy and paste due to reasons. :( ..
0

I think this is what you want:

index = []

def add_to_index(index,keyword,url):
    flag = 0
    count = 0
    for lists in index:        
        if lists[0] == keyword: 
            lists[1].append(url)
            flag = 1
        count += 1

    if flag == 0:
        index.append([keyword, [url]])   

#calling the function below

add_to_index(index,'google','http://google.com')
print index

Comments

0

I will suggest the use of dictionaries for this:

index = {}

def add_to_index(index, keyword, url):
    if keyword not in index:
        index[keyword] = [url]
    else:
        index[keyword].append(url)


>>> add_to_index(index,'computing','http://acm.org')
>>> add_to_index(index,'google','http://gmail.com') 
>>> add_to_index(index,'google','http://gmail.com') 
>>> index
{'computing': ['http://acm.org'], 'google': ['http://gmail.com', 'http://gmail.com']}

You could even make index a non global variable by implementing a simple class(ofcourse, this is possible with nested lists too):

class Index(object):

    def __init__(self):
        self.index = {}

    def add_to_index(self, keyword, url):
        if keyword not in index:
            self.index[keyword] = [url]
        else:
            self.index[keyword].append(url)

Comments

0

First, you're trying to append to a string that is inside of the list you want to append to. Then, you forgot to say flag = 1 when you've found the keyword. Try the following:

index = []

def add_to_index(index,keyword,url):
    flag = 0
    count = 0
    for lists in index:
        if(lists[0]==keyword): 
            index[count][1].append(url)
            flag = 1
    count += 1
    if(flag ==0):
        index.append([keyword,url])   

#calling the function below

add_to_index(index,'google','http://google.com')
add_to_index(index,'computing','http://acm.org')
add_to_index(index,'google','http://gmail.com') 
print index

I think you'd be much better off using a defaultdict, though. It automatically searches for a keyword and adds the item to the existing keyword, or if it isn't found, creates a new keyword.

Comments

0

You could simplify things a little by getting rid of the flag and count variables.

index = []

def add_to_index(index, keyword, url):
    for e in index:
        if e[0] == keyword:
            e[1].append(url)
            return

        else:
            index.append([keyword,[url]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.