1

我已经进行了一堆成对比较(准确地说是 241 x 241)。
生成的文件如下所示:

A,X,10
A,Y,20
X,Y,15

我想将其转换为显示所有成对比较的表格。即这样的东西

,A,X,Y
A,,10,20
X,10,,15
Y,20,15,,

我什至不知道如何开始解决这个问题。任何帮助,建议将不胜感激!

4

2 回答 2

2

将配对数据存储到字典中:

result_dict = {
    (A, X): 10,
    (A, Y): 20,
    (X, Y): 15,
}

和行/列标签:

cols = sorted(set(a for a, b in result_dict.iterkeys()))
rows = sorted(set(b for a, b in result_dict.iterkeys()))

那么这就是你要打印行的方式......

for b in rows:
    row = list(result_dict.get((a, b), None) for a in cols)
    # print the row
于 2012-12-12T01:51:22.080 回答
1

我觉得有更有效的方法,但你可以试一试。它使用csv模块来加载/解析您的数据,然后也将其写回csv(假设您希望在文件中输出,即 - 如果没有,可以调整):

import csv
from collections import defaultdict

# Open the input file and read it all into a defaultdict
with open('table.csv', 'rb') as f:
  reader = csv.reader(f)

  # Dictionary to hold the nested values
  # This will be keyed by the letter ID, and each line of
  # the file will be written twice (so A,X,10 will be represented
  # as {'A': {'X': 10}, 'X': {'A': 10}}
  d = defaultdict(dict)
  for row in reader:
    d[row[0]][row[1]] = row[2]
    d[row[1]][row[0]] = row[2]

# Now we open the output file and write out our defaultdict
with open('t_out.csv', 'wb') as o:
  # Here our fieldnames will start with the 'empty' first header
  # and then be comprised of the keys of the dictionary (which
  # should contain all possible values for the table)
  fieldnames = [' '] + d.keys()
  writer = csv.DictWriter(o, fieldnames=fieldnames)

  # In Python 2.7, you can use writer.writeheaders()
  writer.writerow(dict((h, h) for h in fieldnames))

  # Now iterate through our dictionary, creating a row
  # dictionary that will contain the information to be written
  for k, v in d.iteritems():
    # Here we are putting the key in the 'empty' first column
    v[' '] = k
    writer.writerow(v)
于 2012-12-12T01:51:32.983 回答