用python编写一个脚本,该脚本通过Unix列出相邻的单词? [英] Make a script in python that lists adjacent words through Unix?

查看:87
本文介绍了用python编写一个脚本,该脚本通过Unix列出相邻的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过嵌套字典在python中编写脚本,该脚本需要将txt文件写为

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

以此类推

嗯,我没有发布我所拥有的东西,因为它相当虚假……如果我发现其他问题,我将对其进行更新……我只是被卡住了一段时间- 我所知道的怎么做的是将每个单词/字母放在单独的行上,并带有:

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

我想我想要的是在每个单词中都有某种for循环,如果该单词不在原始词典中,则将其添加到其中,然后搜索出现的单词在文件旁边.

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.

推荐答案

输入

a,c,f,g,hi,lw:1

f,g,j,ew,f,h,a,w:3

fd,s,f,g,s:4

代码

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)

输出

{'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).

简单的解决方案:

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

但这确实改变了顺序.

这篇关于用python编写一个脚本,该脚本通过Unix列出相邻的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆