1

我想要实现的是一个 csv 文件,如下所示:

,h.r9,h.r8,h.r5,h.r4,h.r7,h.r6,h.r1,h.r3,h.r2,h.r10
chris,0,1,0,0,0,0,0,1,0,0
bob,0,0,1,1,1,1,0,0,0,1
dick,1,0,1,0,0,0,0,0,1,1
ab,1,2,0,0,0,0,0,0,0,0
emma,0,0,1,0,1,0,1,0,1,0

有点不相关:有谁知道这样的矩阵的实际名称是什么?这至少会使谷歌搜索这个问题更容易。

数据最初在这样的字典中:

dict = {}
dict['ab'] = ["h.r1","h.r2"]
dict['bob'] = ["h.r3","h.r4","h.r5","h.r6","h.r10"]
dict['chris'] = ["h.r2","h.r8"]
dict['dick'] = ["h.r1","h.r10","h.r9","h.r3"]
dict['emma'] = ["h.r3","h.r5","h.r7","h.r9"]

因此,如果一个人在字典中有变量,则 CSV 中应该是 1,否则应该是零。

下面的代码构建 csv 文件,但全为零。我将如何实现 1 和 0 的逻辑?我可以在 PHP/HTML 中做到这一点,但是对于 Python 来说非常陌生,我并没有赶上......还有一个库可能已经内置了这个函数吗?(也许是numpy?)

import csv

dict = {}
dict['ab'] = ["h.r1","h.r2"]
dict['bob'] = ["h.r3","h.r4","h.r5","h.r6","h.r10"]
dict['chris'] = ["h.r2","h.r8"]
dict['dick'] = ["h.r1","h.r10","h.r9","h.r3"]
dict['emma'] = ["h.r3","h.r5","h.r7","h.r9"]

mergedlist = []
for k,v in dict.items():
        mergedlist = mergedlist + v
mergedlist = list(set(mergedlist))
listlen = len(mergedlist)
mergedlist[0:0] = " "

thekeys = []
for k in dict.keys():
        thekeys.append(k)

thecsv = csv.writer(open("your.csv", 'w', newline=''))
thecsv.writerow(mergedlist)

for col in thekeys:
        thecsv.writerow([col] + ['0']*listlen) #does the logic go in here somehow??
4

3 回答 3

3

我不知道大部分代码在做什么。只需几行即可完成:

all_values = set()
for person_values in data.values():
    all_values = all_values.union(person_values)
all_values_list = list(all_values)

thecsv = csv.writer(open("your.csv", 'w'))
thecsv.writerow(all_values_list)
for name, values in data.items():
    row = [1 if i in values else 0 for i in all_values_list]
    thecsv.writerow([name] + row)

请注意,我已将您的字典重命名为data,因为覆盖内置函数/类名称不是一个好主意。

于 2013-08-24T21:44:04.197 回答
1

我会完全不同,但使用您的代码我会将其更改['0']*listlen为:
['1' if x in dict[col] else '0' for x in thekeys]

于 2013-08-24T21:47:32.063 回答
1

首先,您不想在数据dict覆盖dictionary时为其命名,请将其更改为dict1

#first generate the data body:
>>> ar=[[dict1[jtem].count(item) for item in ['h.r%s' %i for i in range(1, 11)]] for jtem in dict1] #are your values only in h.s1 to h.s10?
>>> ar
[[0, 1, 0, 0, 0, 0, 0, 1, 0, 0], \
[1, 0, 1, 0, 0, 0, 0, 0, 1, 1], \
[0, 0, 1, 1, 1, 1, 0, 0, 0, 1], \
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0], \
[0, 0, 1, 0, 1, 0, 1, 0, 1, 0]]
#Then insert the names:
>>> junk=[item.insert(0, jtem) for item, jtem in zip(ar, dict1.keys())]
>>> ar
[['chris', 0, 1, 0, 0, 0, 0, 0, 1, 0, 0], \
['dick', 1, 0, 1, 0, 0, 0, 0, 0, 1, 1], \
['bob', 0, 0, 1, 1, 1, 1, 0, 0, 0, 1], \
['ab', 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], \
['emma', 0, 0, 1, 0, 1, 0, 1, 0, 1, 0]]
#Then insert the title:
>>> ar.insert(0, ['',]+['h.r%s' %i for i in range(1, 11)])
>>> ar
[['', 'h.r1', 'h.r2', 'h.r3', 'h.r4', 'h.r5', 'h.r6', 'h.r7', 'h.r8', 'h.r9', 'h.r10'], \
['chris', 0, 1, 0, 0, 0, 0, 0, 1, 0, 0], \
['dick', 1, 0, 1, 0, 0, 0, 0, 0, 1, 1], \
['bob', 0, 0, 1, 1, 1, 1, 0, 0, 0, 1], \
['ab', 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], \
['emma', 0, 0, 1, 0, 1, 0, 1, 0, 1, 0]]

然后使用您喜欢的方法将其写入 CSV 文件。

于 2013-08-24T22:10:18.280 回答