python - 列出字典

标签 python list dictionary

在列表中追加是可能的。但是我如何实现在字典中追加?

    Symbols from __ctype_tab.o:
Name                  Value   Class        Type         Size     Line  Section
__ctype             |00000000|   D  |            OBJECT|00000004|     |.data
__ctype_tab         |00000000|   r  |            OBJECT|00000101|     |.rodata

Symbols from _ashldi3.o:
Name                  Value   Class        Type         Size     Line  Section
__ashldi3           |00000000|   T  |              FUNC|00000050|     |.text

Symbols from _ashrdi3.o:
Name                  Value   Class        Type         Size     Line  Section
__ashrdi3           |00000000|   T  |              FUNC|00000058|     |.text

Symbols from _fixdfdi.o:
Name                  Value   Class        Type         Size     Line  Section
__fixdfdi           |00000000|   T  |              FUNC|0000004c|     |.text
__fixunsdfdi        |        |   U  |            NOTYPE|        |     |*UND*

我怎样才能创建一个像这样的字典:

dictOfTables {'__ctype_tab.o':{'__ctype': Name:...,Value:...,Class:...,Type:...,Size:...,Line:...,Section:...}} etc.

对于上面的文字?

最佳答案

追加对于字典的概念没有意义,就像列表一样。相反,更明智的说法是插入和删除键/值,因为没有要附加的“end”——字典是无序的。

从你想要的输出来看,你似乎想要一个字典的字典,(即 {filename : { symbol : { key:value }}。我想你可以得到这个从你的输入中得到这样的东西:

import re

header_re = re.compile('Symbols from (.*):')

def read_syms(f):
    """Read list of symbols from provided iterator and return dict of values"""
    d = {}
    headings=None
    for line in f:
        line = line.strip()
        if not line: return d  # Finished.

        if headings is None:
             headings = [x.strip() for x in line.split()]
             continue # First line is headings

        items = [x.strip() for x in line.split("|")]
        d[items[0]] = dict(zip(headings[1:], items[1:]))
    return d

f=open('input.txt')
d={}
for line in f:
    m=header_re.match(line)
    if m:
        d[m.group(1)] = read_syms(f)

关于python - 列出字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/828578/

相关文章:

python - 使用 == 比较 numpy 数组的规则是什么?

python - 大矩阵上的余弦相似度

ios - 找不到接受提供的参数的 'subscript' 的重载

python - 在 Python 中填充空字典

Python 将字典视为字符串

在 2D numpy 数组中加入近端段的 Pythonic 方法?

python - 如何覆盖 sympy.core.numbers.Float 的 __str__ 方法?

python - 从列表中创建路径文件的名称

python - 按日期对对象列表进行排序,其中对象没有名称

c# - C# 中是否有任何方法或方式可以在 LIFO 的基础上添加 List<T> 中的项目?