Make a script in python that lists adjacent words through Unix?
Question: How can I write a script in python through nested dictionaries that takes a
`txt` file written as,

    
    
    white,black,green,purple,lavendar:1
    
    red,black,white,silver:3
    
    black,white,magenta,scarlet:4
    

and make it print for each entry before the **:** character, all neighbors it
showed up next to

    
    
    white: black silver magenta
    
    black: white green red 
    
    green: black purple
    

and so on

Edit: Well, I didn't post what I have because it is rather
unsubstantial...I'll update it if I figure out anything else... I just have
been stuck for a while - all I have figured out how to do is post each
word/letter on a separate line with:

    
    
    from sys import argv
    script,filename=argv
    txt=open(filename)
    for line in txt:
        line=line[0:line.index(';')]
        for word in line.split(","):
            print word
    

I guess what I want is to have some kind of for loop that runs through each
word, if the word is not in an original dictionary, I'll add it to it, then
I'll search through for words that appear next to it in the file.


Answer: **Input**

    
    
    a,c,f,g,hi,lw:1
    
    f,g,j,ew,f,h,a,w:3
    
    fd,s,f,g,s:4
    

**Code**

    
    
    neighbours = {}
    
    for line in file('4-input.txt'):
        line = line.strip()
        if not line:
            continue    # skip empty input lines
    
        line = line[:line.index(':')]   # take everything left of ':'
    
        previous_token = ''
        for token in line.split(','):
            if previous_token:
                neighbours.setdefault(previous_token, []).append(token)
                neighbours.setdefault(token, []).append(previous_token)
            previous_token = token
    
        import pprint
        pprint.pprint(neighbours)
    

**Output**

    
    
    {'a': ['c', 'h', 'w'],
    'c': ['a', 'f'],
    'ew': ['j', 'f'],
    'f': ['c', 'g', 'g', 'ew', 'h', 's', 'g'],
    'fd': ['s'],
    'g': ['f', 'hi', 'f', 'j', 'f', 's'],
    'h': ['f', 'a'],
    'hi': ['g', 'lw'],
    'j': ['g', 'ew'],
    'lw': ['hi'],
    's': ['fd', 'f', 'g'],
    'w': ['a']}
    

Tidying up the prettyprinted dictionary is left as an exercise for the reader.
(Because dictionaries are inherently not sorted into any order, and removing
the duplicates without changing the ordering of the lists is also annoying).

Easy solution:

    
    
    for word, neighbour_list in neighbours.items():
        print word, ':', ', '.join(set(neighbour_list))
    

But that does change the ordering.

