Search code examples
pythonlistdictionarywordnet

using an integer in a string to create a dictionary (or list) with that many numbers


so i have this text (wordnet) file made up of numbers and words, for example like this -

"09807754 18 n 03 aristocrat 0 blue_blood 0 patrician"

and i want to read in the first number as a dictionary name (or list) for the words that follow. the layout of this never changes, it is always an 8 digit key followed by a two digit number, a single letter and a two digit number. This last two digit number (03) tells how many words (three words in this case) are associated with the first 8 digit key.

my idea was that i would search for the 14th place in the string and use that number to run a loop to pick in all of the words associated with that key

so i think it would go something like this

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

Technically i am looking for whichever one of these makes more sense:

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

Obviously this doesnt run but if anyone could give me any pointers it would be greatly appreciated


Solution

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

    For the extra line in your comment, some extra logic is needed

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