-1

我想找到一列的重复值并替换为具有多列的另一列 csv 的值。所以首先我将两列从 csv 放到字典中。然后我想找到具有字符串值和键的字典的重复值。我尝试了删除重复字典的解决方案,但得到的错误是不可散列或没有结果。这是代码的第一部分。

import csv
from collections import defaultdict
import itertools as it

mydict = {}
index = 0
reader = csv.reader(open(r"computing.csv", "rb"))
for i, rows in enumerate(reader):
    if i == 0: 
    continue

    if len(rows) == 0:
        continue
    k = rows[3].strip()      
    v = rows[2].strip()    
    if k in mydict:        
        mydict[k].append(v)
    else:
        mydict[k] = [v]

#mydict = hash(frozenset(mydict))

print mydict

d = {}
while True:
    try:        
        d = defaultdict(list)
        for k,v in mydict.iteritems():
            #d[frozenset(mydict.items())]
            d[v].append(k)
    except:
        continue

writer = csv.writer(open(r"OLD.csv", 'wb'))
for key, value in d.items():
    writer.writerow([key, value])
4

1 回答 1

2

你的问题不清楚。所以我希望我做对了。

请给出输入列和所需输出列的示例。请打印出错误并让我们知道是哪一行导致了错误。

如果你希望输出是column1=[1,2,3,1,4]column2=[a,b,c,d,e]n_column1=[a,2,3,d,4]column2 =[1,b,c,d,e]

我想例外是d[v].append(k)因为显然 v 是一个列表。您不能将列表用作字典中的键。

In [1]: x = [1,2,3,1,4]

In [2]: y = ['a','b','c','d','e']

In [5]: from collections import defaultdict

In [6]: d = defaultdict(int)

In [7]: for a in x:
   ...:     d[a] += 1


In [8]: d
Out[8]: defaultdict(<type 'int'>, {1: 2, 2: 1, 3: 1, 4: 1})

In [9]: x2 = []

In [10]: for a,b in zip(x,y):
   ....:     x2.append(a if d[a]==1 else b)
   ....:     

In [11]: x
Out[11]: [1, 2, 3, 1, 4]

In [12]: x2
Out[12]: ['a', 2, 3, 'd', 4]

在那种情况下,我想我是否必须更改您的代码以适应。我会做这样的事情:

import csv
from collections import defaultdict
import itertools as it

mydict = {}
index = 0
reader = csv.reader(open(r"computing.csv", "rb"))
histogram = defaultdict(int)
k = []
v = []
for i, rows in enumerate(reader):
    if i == 0: 
        continue

    if len(rows) == 0:
        continue
    k.append(rows[3].strip())
    v.append(rows[2].strip())

    item = k[-1]
    histogram[item] += 1

output_column = []

for first_item, second_item in zip(k,v):
    output_column.append(first_item if histogram[first_item]==1 else second_item)

writer = csv.writer(open(r"OLD.csv", 'wb'))
for c1, c2 in zip(output_column, v):
    writer.writerow([c1, c2])
于 2013-05-14T07:35:40.983 回答