在python中向字典添加符号后的节

2024-06-16 06:36:40 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我需要读一行字,然后放到字典里,第一个数字是关键字,第四个数字是与关键字相关联的单词数量。你知道吗

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']}

Tags: orofthecityline数字关键字member
2条回答
with open("wordnetSample.txt") as f:
    d = {}
    for line in f:
        data, label = line.split(' | ')
        field = data.split()
        d[field[0]] = [field[4], label]

就是这样

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'|'

相关问题 更多 >