gpt4 book ai didi

python - 在 python 中的字典中添加符号后面的部分

转载 作者:行者123 更新时间:2023-12-01 05:43:10 26 4
gpt4 key购买 nike

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

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

最佳答案

这样做了

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

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

关于python - 在 python 中的字典中添加符号后面的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16943190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com