Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将元素附加到嵌套列表中的列表-python_Python_List_Nested Lists - Fatal编程技术网

将元素附加到嵌套列表中的列表-python

将元素附加到嵌套列表中的列表-python,python,list,nested-lists,Python,List,Nested Lists,我正在开发一个将_添加到_索引的过程,该过程需要3个输入: 索引:[,[url1,url2,…],…] 关键词:字符串 url:String 如果关键字已在索引中,则url将添加到与该关键字关联的url列表中 如果关键字不在索引中,则索引中将添加新元素: [keyword,[url]] 代码 输出->[['google','http://google.com']] add_to_index(index,'computing','http://acm.org') print index a

我正在开发一个将_添加到_索引的过程,该过程需要3个输入:

  • 索引:[,[url1,url2,…],…]
  • 关键词:字符串
  • url:String
如果关键字已在索引中,则url将添加到与该关键字关联的url列表中

如果关键字不在索引中,则索引中将添加新元素:

[keyword,[url]]
代码

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

add_to_index(index,'computing','http://acm.org')
print index
add_to_index(index,'google','http://gmail.com') 
print index
输出->
[['google','http://google.com“],[‘计算’,”http://acm.org']]

add_to_index(index,'computing','http://acm.org')
print index
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']]]

您犯了三个错误,第一个错误是没有使用
标志,第二个错误是将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
变成一个非全局变量:


您可以通过去掉标志和计数变量来简化一些事情

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

似乎这是一个错误的数据结构:你最好使用字典和dictbuilder。这是我在一次竞争性编程竞赛中得到的一个测验。犯三个错误-
count
应该在
for
循环的末尾递增。@Kaivosukeltaja如果你感觉不好,我很抱歉,但我花了三分钟写下了你的名字,因为种种原因我无法复制和粘贴( ..