Add item to list inside nested list - python

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

#calling the function below

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']]]
+4
source share
5 answers

: -, flag, -, URL- . , , Kaivosuketaja , . :

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

[['google', ['http://google.com']]]
[['google', ['http://google.com']], ['computing', ['http://acm.org']]]
[['google', ['http://google.com', 'http://gmail.com']], ['computing', ['http://acm.org']]]
+3

, , . flag = 1, . :

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

, defaultdict. , , .

0

, , :

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
0

:

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']}

index , (, ):

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

You can simplify things a bit by getting rid of the flags 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]])
0
source

All Articles