1

所以我有一行需要阅读并放入字典中,第一个数字是键,第四个数字是与键相关联的单词数量。

f = open("wordnetSample.txt", "r")
D = {}
for line in f:
    L = line.split()
    D.update({L[0]: L[4:4 + 2 * int(L[3]):2]})

这些是我要放入字典的行的示例

09826802 18 n 01 Areopagite 0 002 @ 10326901 n 0000 #m 08181009 n 0000 | a member of  the council of the Areopagus  
09826918 18 n 01 Argive 0 002 @ 09729560 n 0000 + 08804512 n 0101 | a native or inhabitant of the city of Argos  

这就是我迄今为止所拥有的 D

{'09826802': ['Areopagite'], '09826918': ['Argive']}

我想要这个:

{'09826802': ['Areopagite', 'a member of  the council of the Areopagus'], '09826918': ['Argive', 'a native or inhabitant of the city of Argos']}
4

2 回答 2

1

这是在做

D = {}
for line in f:
    L = line.split()
    L2 = line.split('|')
    D.update({L[0]: (L[4:4 + 2 * int(L[3]):2][0], L2[1].split('\n')[0])})

我添加了另一个拆分 wrt'|'

于 2013-06-05T15:07:21.600 回答
1
with open("wordnetSample.txt") as f:
    d = {}
    for line in f:
        data, label = line.split(' | ')
        field = data.split()
        d[field[0]] = [field[4], label]
于 2013-06-05T15:10:35.670 回答