2009-05-06 5 views
1

목록에서 추가가 가능합니다. 하지만 사전에 추가하는 방법은 무엇입니까?사전 목록

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. 

를?

답변

1

ordered dictionary을 사용하십시오. 이것이 공식 Python에서는 아직 생각하지 않지만 PEP에서 사용할 수있는 참조 구현이 있습니다.

7

목록과 같은 방식으로 사전의 개념에 붙이는 것은 의미가 없습니다. 대신, 키/값을 삽입하고 제거하는 측면에서 말하면 더 합리적입니다. "end"을 추가 할 필요가 없습니다. 사전은 순서가 지정되지 않습니다. 당신은 dicts의 dicts의 DICT를 갖고 싶어처럼 원하는 출력에서 ​​

, 그것은 보인다 (즉, {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) 
+0

최종 DICT { '_ashrdi3.o': { '__ashrdi3': { '섹션': '. 텍스트', '값': '00000000', '라인': '', '유형': 'FUNC ','Class ':'T ','Size ':'00000058 '}}, '_ashldi3.o ': {'__ashldi3 ': {'섹션 ':'.text ','값 ':'00000000 ' '유형': 'FUNC', '클래스': 'T', '크기': '00000050'}}, '_fixdfdi.o': { '__fixdfdi': { '섹션' '. 텍스트', '값': '00000000', '섹션': '* UND *', '값' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '''0000004c} : '', 'Line': '', 'Type': 'NOTYPE', 'Class': 'U', 'Size': ''}}} – flight

관련 문제