0

所以我有这个由数字和单词组成的文本(wordnet)文件,例如像这样 -

"09807754 18 n 03 aristocrat 0 blue_blood 0 patrician"

我想读入第一个数字作为后面单词的字典名称(或列表)。它的布局永远不会改变,它始终是一个 8 位键,后跟一个两位数、一个字母和一个两位数。最后两位数字 (03) 表示有多少单词(在本例中为三个单词)与前 8 位密钥相关联。

我的想法是,我将搜索字符串中的第 14 位并使用该数字运行一个循环以选择与该键关联的所有单词

所以我认为它会像这样

with open('nouns.txt','r') as f:
    for line in f:

        words = range(14,15)
        numOfWords = int(words)
            while i =< numOfWords
                #here is where the problem arises, 
                #i want to search for words after the spaces 3 (numOfWords) times 
                #and put them into a dictionary(or list) associated with the key
                range(0,7) = {word(i+1), word(i+2)}

从技术上讲,我正在寻找其中更有意义的一个:

09807754 = { 'word1':aristocrat, 'word2':blue_blood , 'word3':patrician }
or
09807754 = ['aristocrat', 'blue_blood', 'patrician']

显然这不会运行,但如果有人可以给我任何指示,将不胜感激

4

2 回答 2

5
>>> L = "09807754 18 n 03 aristocrat 0 blue_blood 0 patrician".split()
>>> L[0], L[4::2]
('09807754', ['aristocrat', 'blue_blood', 'patrician'])

>>> D = {}
>>> D.update({L[0]: L[4::2]})
>>> D
{'09807754': ['aristocrat', 'blue_blood', 'patrician']}

对于评论中的额外行,需要一些额外的逻辑

>>> L = "09827177 18 n 03 aristocrat 0 blue_blood 0 patrician 0 013 @ 09646208 n 0000".split()
>>> D.update({L[0]: L[4:4 + 2 * int(L[3]):2]})
>>> D
{'09807754': ['aristocrat', 'blue_blood', 'patrician'], '09827177': ['aristocrat', 'blue_blood', 'patrician']}
于 2013-06-05T11:35:31.573 回答
0
res = {}
with open('nouns.txt','r') as f:
    for line in f:
        splited = line.split()
        res[splited[0]] = [w for w in splited[4:] if not w.isdigit()] 

输出:

{'09807754': ['aristocrat', 'blue_blood', 'patrician']}
于 2013-06-05T11:40:45.750 回答