2D表格格式Python

2D表格格式Python,python,2d,tabular,Python,2d,Tabular,我想从存储在字典中的数据创建2D表格格式 例如: d = {'ID1':[('Experiment1', 40), (Experiment2, 59), (Experiment3, 65)], 'ID2':[('Experiment1', 68), (Experiment2, 21), (Experiment3, 39)], 'ID3':[('Experiment1', 57), (Experiment2, 15), (Experiment4, 99)]} 应给出下表:

我想从存储在字典中的数据创建2D表格格式

例如:

d = {'ID1':[('Experiment1', 40), (Experiment2, 59), (Experiment3, 65)],    
 'ID2':[('Experiment1', 68), (Experiment2, 21), (Experiment3, 39)],   
 'ID3':[('Experiment1', 57), (Experiment2, 15), (Experiment4, 99)]}
应给出下表:

    Experiment1     Experiment2     Experiment3     Experiment4
ID1…40…59…65。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 ID2.68.21.39。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 ID3…57…15…99

其中IDx是行标签,Experimentx是列名。 如果数据集的列名没有值,则应添加一个空字符串作为占位符


有人能帮我怎么做吗?python中是否有我可以使用的现成格式?

一个快速而肮脏的实现,可以修复问题中的输入错误

d = {'ID1':[('Experiment1', 40), ('Experiment2', 59), ('Experiment3', 65)],    
 'ID2':[('Experiment1', 68), ('Experiment2', 21), ('Experiment3', 39)],   
 'ID3':[('Experiment1', 57), ('Experiment2', 15), ('Experiment4', 99)]}

COLUMN_WIDTH = 20
STRING_WHEN_MISSING = '""'
PADDING_STRING = "."


#first determine what are the columns - from the first line
columns = ["id"] #there is always at least the id column
columns = columns + [colName for (colName, colValue) in d.items()[0][1]]

print "".join([ col.ljust(COLUMN_WIDTH) for col in columns])

#browse the lines, order by ID Ascending
for (rowId, rowValues) in sorted(d.items(), key= lambda x: x[0].lower()):
    #print rowId
    #print rowValues
    rowValuesDict = dict(rowValues) #make it a dict with access by key
    rowValuesDict["id"] = rowId
    #print rowValuesDict
    print "".join([ str(rowValuesDict.get(col, STRING_WHEN_MISSING)).ljust(COLUMN_WIDTH, PADDING_STRING) for col in columns])
这将打印出:

id                  Experiment1         Experiment2         Experiment3         
ID1.................40..................59..................65..................
ID2.................68..................21..................39..................
ID3.................57..................15..................""..................
注: 你的原始词典的格式有点奇怪。。。我会期待更多类似的事情:

d = [('ID1',{'Experiment1': 40, 'Experiment2':59, 'Experiment3':65}),    
     ('ID2',{'Experiment1': 68, 'Experiment2':21, 'Experiment3':39})] 
一些评论: 对于这类内容,您需要了解一些Python字符串方法:它们在字符串之前和/或之后添加字符以强制其总宽度

显然,这个想法主要是通过列表/字典循环并提取值


注意dict方法的使用,该方法允许在给定键不存在值时使用默认值。

一个快速而肮脏的实现修复问题中的键入错误

d = {'ID1':[('Experiment1', 40), ('Experiment2', 59), ('Experiment3', 65)],    
 'ID2':[('Experiment1', 68), ('Experiment2', 21), ('Experiment3', 39)],   
 'ID3':[('Experiment1', 57), ('Experiment2', 15), ('Experiment4', 99)]}

COLUMN_WIDTH = 20
STRING_WHEN_MISSING = '""'
PADDING_STRING = "."


#first determine what are the columns - from the first line
columns = ["id"] #there is always at least the id column
columns = columns + [colName for (colName, colValue) in d.items()[0][1]]

print "".join([ col.ljust(COLUMN_WIDTH) for col in columns])

#browse the lines, order by ID Ascending
for (rowId, rowValues) in sorted(d.items(), key= lambda x: x[0].lower()):
    #print rowId
    #print rowValues
    rowValuesDict = dict(rowValues) #make it a dict with access by key
    rowValuesDict["id"] = rowId
    #print rowValuesDict
    print "".join([ str(rowValuesDict.get(col, STRING_WHEN_MISSING)).ljust(COLUMN_WIDTH, PADDING_STRING) for col in columns])
这将打印出:

id                  Experiment1         Experiment2         Experiment3         
ID1.................40..................59..................65..................
ID2.................68..................21..................39..................
ID3.................57..................15..................""..................
注: 你的原始词典的格式有点奇怪。。。我会期待更多类似的事情:

d = [('ID1',{'Experiment1': 40, 'Experiment2':59, 'Experiment3':65}),    
     ('ID2',{'Experiment1': 68, 'Experiment2':21, 'Experiment3':39})] 
一些评论: 对于这类内容,您需要了解一些Python字符串方法:它们在字符串之前和/或之后添加字符以强制其总宽度

显然,这个想法主要是通过列表/字典循环并提取值


请注意,使用dict方法可以在给定键不存在值的情况下使用默认值。

感谢这个注释良好的示例和get提示。我不知道这一点,或者干脆忽略了这一点。这实际上是主要问题……不客气:现在我看到一个问题,如果第一行没有包含所有可能列的值,那么该列将不会显示在输出中。您可能需要第一次遍历所有行,并找出所有可能的列。。。或者,如果列列表是静态的,您也可以对其进行硬编码。感谢这个注释很好的示例和get提示。我不知道这一点,或者干脆忽略了这一点。这实际上是主要问题……不客气:现在我看到一个问题,如果第一行没有包含所有可能列的值,那么该列将不会显示在输出中。您可能需要第一次遍历所有行,并找出所有可能的列。。。或者,如果列列表是静态的,则可以对其进行硬编码