减少Python列表中的重复项

减少Python列表中的重复项,python,multidimensional-array,Python,Multidimensional Array,我正在编写一个程序,它读入许多文件,然后对其中的术语进行索引。我能够在python中将文件读入2d数组(列表),但是接下来我需要删除第一列中的重复项,并将索引存储在一个新列中,第一次出现重复的单词 例如: ['when', 1] ['yes', 1] ['', 1] ['greg', 1] ['17', 1] ['when',2] 第一列是术语,第二列是它来自的DocID 我希望能够将此更改为: ['when', 1, 2] ['yes', 1] ['', 1] ['greg', 1] ['1

我正在编写一个程序,它读入许多文件,然后对其中的术语进行索引。我能够在python中将文件读入2d数组(列表),但是接下来我需要删除第一列中的重复项,并将索引存储在一个新列中,第一次出现重复的单词

例如:

['when', 1]
['yes', 1]
['', 1]
['greg', 1]
['17', 1]
['when',2]
第一列是术语,第二列是它来自的DocID 我希望能够将此更改为:

['when', 1, 2]
['yes', 1]
['', 1]
['greg', 1]
['17', 1]
删除副本

这就是我到目前为止所做的:

for j in range(0,len(index)):
        for r in range(1,len(index)):
                if index[j][0] == index[r][0]:
                        index[j].append(index[r][1])
                        index.remove(index[r])
我一直在收到一个超出范围的错误

if index[j][0] == index[r][0]:
我想这是因为我从索引中删除了一个对象,所以它变小了。任何想法都将不胜感激
(是的,我知道我不应该修改原始版本,但这只是在小规模上进行测试)

构建一个/

比如:

from collections import defaultdict

ar = [['when', 1],
      ['yes', 1],
      ['', 1],
      ['greg', 1],
      ['17', 1],
      ['when',2]] 

result = defaultdict(list)
for lst in ar:
    result[lst[0]].append(lst[1])
输出:

>>> for k,v in result.items():
...     print(repr(k),v)
'' [1]
'yes' [1]
'greg' [1]
'when' [1, 2]
'17' [1]

是不是更适合建立一个/

比如:

from collections import defaultdict

ar = [['when', 1],
      ['yes', 1],
      ['', 1],
      ['greg', 1],
      ['17', 1],
      ['when',2]] 

result = defaultdict(list)
for lst in ar:
    result[lst[0]].append(lst[1])
输出:

>>> for k,v in result.items():
...     print(repr(k),v)
'' [1]
'yes' [1]
'greg' [1]
'when' [1, 2]
'17' [1]

是的,您的错误来自修改列表。此外,您的解决方案对于长列表无效。最好改用字典,并在末尾将其转换回列表:

from collections import defaultdict
od = defaultdict(list)

for term, doc_id in index:
    od[term].append(doc_id)

result = [[term] + doc_ids for term, doc_ids in od.iteritems()]

print result
# [['', 1], ['yes', 1], ['greg', 1], ['when', 1, 2], ['17', 1]]

是的,您的错误来自修改列表。此外,您的解决方案对于长列表无效。最好改用字典,并在末尾将其转换回列表:

from collections import defaultdict
od = defaultdict(list)

for term, doc_id in index:
    od[term].append(doc_id)

result = [[term] + doc_ids for term, doc_ids in od.iteritems()]

print result
# [['', 1], ['yes', 1], ['greg', 1], ['when', 1, 2], ['17', 1]]

实际上,您可以使用
range()
len()
来完成此操作。然而,python的妙处在于,您可以直接迭代列表中的元素,而无需索引

看看这段代码,试着理解它

#!/usr/bin/env python

def main():

    tot_array = \
    [ ['when', 1],
      ['yes', 1],
      ['', 1],
      ['greg', 1],
      ['17', 1],
      ['when',2]
    ]

    for aList1 in tot_array:
        for aList2 in tot_array:
            if aList1[0]==aList2[0] and aList1 !=aList2:
                aList1.append(aList2[1])
                tot_array.remove(aList2)
    print tot_array

    pass

if __name__ == '__main__':
    main()
输出将如下所示:

*** Remote Interpreter Reinitialized  ***
>>> 
[['when', 1, 2], ['yes', 1], ['', 1], ['greg', 1], ['17', 1]]

实际上,您可以使用
range()
len()
来完成此操作。然而,python的妙处在于,您可以直接迭代列表中的元素,而无需索引

看看这段代码,试着理解它

#!/usr/bin/env python

def main():

    tot_array = \
    [ ['when', 1],
      ['yes', 1],
      ['', 1],
      ['greg', 1],
      ['17', 1],
      ['when',2]
    ]

    for aList1 in tot_array:
        for aList2 in tot_array:
            if aList1[0]==aList2[0] and aList1 !=aList2:
                aList1.append(aList2[1])
                tot_array.remove(aList2)
    print tot_array

    pass

if __name__ == '__main__':
    main()
输出将如下所示:

*** Remote Interpreter Reinitialized  ***
>>> 
[['when', 1, 2], ['yes', 1], ['', 1], ['greg', 1], ['17', 1]]