如何通过嵌套的字典在python中编写脚本,该字典将txt
文件写为,
white,black,green,purple,lavendar:1
red,black,white,silver:3
black,white,magenta,scarlet:4
并在:字符之前为每个条目打印,其旁边显示的所有邻居
white: black silver magenta
black: white green red
green: black purple
等等
编辑:嗯,我没有发布我所拥有的内容,因为它相当不实际......如果我弄清楚其他任何事情,我会更新它......我只是被困了一段时间 - 所有我想出来的方法是将每个单词/字母张贴在一个单独的行上:
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循环,如果单词不在原始字典中,我会将其添加到它,然后我会搜索出现的单词在文件旁边。
答案 0 :(得分:3)
<强>输入强>
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']}
整理漂亮的字典是留给读者的练习。 (因为字典本身没有按任何顺序排序,删除重复项而不改变列表的顺序也很烦人。)
简易解决方案:
for word, neighbour_list in neighbours.items():
print word, ':', ', '.join(set(neighbour_list))
但这确实改变了顺序。
答案 1 :(得分:0)
你走了:
from collections import defaultdict
char_map = defaultdict(set)
with open('input', 'r') as input_file:
for line in input_file:
a_list, _ = line.split(':') # Discard the stuff after the :
chars = a_list.split(',') # Get the elements before : as a list
prev_char = ""
for char, next_char in zip(chars, chars[1:]): # For every character add the
# next and previous chars to the
# dictionary
char_map[char].add(next_char)
if prev_char:
char_map[char].add(prev_char)
prev_char = char
print char_map
答案 2 :(得分:0)
def parse (input_file):
char_neighbours = {}
File = open(input_file,'rb')
for line in File:
line = line.strip().split(':')[0]
if line != "":
csv_list=line.split(',')
for i in xrange(0,len(csv_list)-1):
value = char_neighbours.get(csv_list[i]) or False
if value is False:
char_neighbours[csv_list[i]] = []
if(i<len(csv_list)):
if str(csv_list[i+1]) not in char_neighbours[str(csv_list[i])]:
char_neighbours[str(csv_list[i])].append(str(csv_list[i+1]))
if(i>0):
if str(csv_list[i-1]) not in char_neighbours[str(csv_list[i])]:
char_neighbours[str(csv_list[i])].append(str(csv_list[i-1]))
return char_neighbours
if __name__ == "__main__":
dictionary=parse('test.txt')
print dictionary
parse方法返回一个字符串字典,其中包含邻居列表作为其值