将列表字典打印为网格布局

Printing a dict of lists as a grid-layout

好的,我之前阅读了所有这些,我认为 pandas 可能是一个解决方案,但我的问题略有不同:
Print a dictionary of lists vertically
Printing Lists as Tabular Data
print dictionary values which are inside a list in python

我有一个列表字典:

dict={"A":[i1, i2,i3], "B":[i1, i4,i5], "C":[i1, i2,i5]}

我想要的输出是:

    i1    i2    i3    i4    i5   
A    x     x     x     -     -   
B    x     -     -     x     x   
C    x     x     -     -     x  

(甚至更好,

    i1    i2    i3    i4    i5  
A    A     A     A     -     -  
B    B     -     -     B     B  
C    C     C     -     -     C  

或另一个字典中匹配A、B、C或(A,in)的值, 但如果我能得到第一个table,我会非常高兴)

没有列表包含重复,但是这些列表中的每个元素都是从同一个列表中提取的(实际上我的问题是用相应的蛋白质制作一个注释术语的网格,键是注释术语,它们是与这些蛋白质在我的研究背景下)。

我确实可以想到一种复杂的方法(构建 0 和 1 的向量以将每个列表与一般列表进行比较,将这些向量与键相关联,将其放入 pandas DataFrame这将被我的魔法很好地格式化,重新建立每个列表的实体数量,并打印这个),但是这个 seems/is tedious/unpythonic.

我认为一定有一种已知的方法可以使用某些模块(pandas、漂亮table、其他?);而我只是不知道。 因此,我很高兴对此有任何见解。谢谢

applylambda

d = {
    "A": ['i1', 'i2', 'i3'],
    "B": ['i1', 'i4', 'i5'],
    "C": ['i1', 'i2', 'i5']
}

df = pd.DataFrame(d)

df.apply(lambda c: pd.Series(c.name, c.values)).fillna('-').T

  i1 i2 i3 i4 i5
A  A  A  A  -  -
B  B  -  -  B  B
C  C  C  -  -  C

只是一个简单的草稿(基于很多str.format):

def create_table(dictionary, columns):
    column_set = set(columns)  # only to speed up "in" calls, could be omitted
    # Fill in the symbols depending on the presence of the corresponding columns
    filled_dct = {key: [' X' if col in lst else ' -' for col in column_set] 
                  for key, lst in dct.items()}

    # A template string that is filled for each row
    row_template = '   '.join(['{}']*(len(columns)+1))

    print(row_template.format(*([' '] + columns)))
    for rowname, rowcontent in sorted(filled_dct.items()):
        print(row_template.format(*([rowname] + rowcontent)))

dct = {"A": ['i1', 'i2', 'i3'], 
       "B": ['i1', 'i4', 'i5'], 
       "C": ['i1', 'i2', 'i5']}

columns = ['i1', 'i2', 'i3', 'i4', 'i5']

create_table(dct, columns)
    i1   i2   i3   i4   i5
A    X    X    -    -    X
B    X    -    X    X    -
C    X    X    X    -    -

虽然不是很灵活(可变列宽等),但应该很容易扩展。

考虑您的输入字典:

dic = {"A":["i1", "i2", "i3"], "B":["i1", "i4", "i5"], "C":["i1", "i2", "i5"]}

使用 dict.fromkeys() 使可迭代成为 dic (a.k.a dic.values()) 中存在的值,这是一个 list 并且它的默认值将是是 dic's 键 (a.k.a dic.keys())。

字典理解 的帮助下,最后一步计算的结果将构成数据框的值。转置它,使列 headers 成为索引轴和 vice-versa.

稍后,用 "-" 填充 Nans

pd.DataFrame({k:dict.fromkeys(v,k) for k,v in dic.items()}).T.fillna("-")
#                               ^----- replace k with "x" to get back the first o/p