簡體   English   中英

將元素追加到嵌套列表中的列表 - python

[英]Append element to a list inside nested list - python

我正在開發一個過程add_to_index,它需要3個輸入:

  • 索引:[[,[url1,url2,...]],...]
  • 關鍵字:字符串
  • 網址:字符串

如果關鍵字已在索引中,則將url添加到與該關鍵字關聯的URL列表中。

如果關鍵字不在索引中,則新元素將出現在索引中:

[keyword,[url]]

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

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

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

輸出 - > [['google', 'http://google.com'], ['computing', 'http://acm.org']]

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

錯誤 - >

index[count][1].append(url)
AttributeError: 'str' object has no attribute 'append'

預期產量:

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

你犯了三個錯誤,首先你沒有使用過這個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']]]

首先,您嘗試附加到要附加到列表內的字符串。 然后,當您找到關鍵字時,忘記說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會好得多。 它會自動搜索關鍵字並將項目添加到現有關鍵字,或者如果找不到,則創建新關鍵字。

我想這就是你想要的:

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

我會建議使用字典:

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)

你可以通過擺脫標志和計數變量來簡化一些事情。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM