用python编写一个脚本,通过Unix列出相邻单词?
如何通过嵌套的字典用python编写脚本,这些字典将用python编写一个脚本,通过Unix列出相邻单词?,python,line,Python,Line,如何通过嵌套的字典用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 等等 编辑:嗯,我没有发布我所
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循环,如果该单词不在原始词典中,我会将其添加到该词典中,然后在文件中搜索出现在该单词旁边的单词。给你:
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
给你:
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
给你:
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
给你:
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
输入
a,c,f,g,hi,lw:1
f,g,j,ew,f,h,a,w:3
fd,s,f,g,s:4
代码
neights={}
对于文件('4-input.txt')中的行:
line=line.strip()
如果不是直线:
继续#跳过空输入行
line=line[:line.index(“:”)]#取“:”左边的所有内容
上一个_标记=“”
对于行中的标记。拆分(','):
如果以前的\u令牌:
setdefault(上一个_标记,[])。append(标记)
setdefault(令牌,[]).append(上一个\u令牌)
前一个令牌=令牌
导入pprint
pprint.pprint(邻居)
输出
a,c,f,g,hi,lw:1
f,g,j,ew,f,h,a,w:3
fd,s,f,g,s:4
{'a':['c','h','w'],
'c':['a','f'],
“ew”:['j','f'],
‘f’:[‘c’、‘g’、‘g’、‘ew’、‘h’、‘s’、‘g’],
'fd':['s'],
‘g’:[f’、‘嗨’、‘f’、‘j’、‘f’、‘s’],
'h':['f','a'],
‘嗨’:['g','lw'],
'j':['g','ew'],
“lw”:[“hi”],
's':['fd','f','g'],
'w':['a']}
整理预印好的词典留给读者作为练习。(因为字典本质上不按任何顺序排序,在不改变列表顺序的情况下删除重复项也很烦人)
简易解决方案:
对于word,邻居\邻居中的列表。项()
打印单词“:”,“,”。连接(集合(邻居列表))
但这确实改变了顺序。输入
a,c,f,g,hi,lw:1
f,g,j,ew,f,h,a,w:3
fd,s,f,g,s:4
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
代码
neights={}
对于文件('4-input.txt')中的行:
line=line.strip()
如果不是直线:
继续#跳过空输入行
line=line[:line.index(“:”)]#取“:”左边的所有内容
上一个_标记=“”
对于行中的标记。拆分(','):
如果以前的\u令牌:
setdefault(上一个_标记,[])。append(标记)
setdefault(令牌,[]).append(上一个\u令牌)
前一个令牌=令牌
导入pprint
pprint.pprint(邻居)
输出
a,c,f,g,hi,lw:1
f,g,j,ew,f,h,a,w:3
fd,s,f,g,s:4
{'a':['c','h','w'],
'c':['a','f'],
“ew”:['j','f'],
‘f’:[‘c’、‘g’、‘g’、‘ew’、‘h’、‘s’、‘g’],
'fd':['s'],
‘g’:[f’、‘嗨’、‘f’、‘j’、‘f’、‘s’],
'h':['f','a'],
‘嗨’:['g','lw'],
'j':['g','ew'],
“lw”:[“hi”],
's':['fd','f','g'],
'w':['a']}
整理预印好的词典留给读者作为练习。(因为字典本质上不按任何顺序排序,在不改变列表顺序的情况下删除重复项也很烦人)
简易解决方案:
对于word,邻居\邻居中的列表。项()
打印单词“:”,“,”。连接(集合(邻居列表))
但这确实改变了顺序。输入
a,c,f,g,hi,lw:1
f,g,j,ew,f,h,a,w:3
fd,s,f,g,s:4
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
代码
neights={}
对于文件('4-input.txt')中的行:
line=line.strip()
如果不是直线:
继续#跳过空输入行
line=line[:line.index(“:”)]#取“:”左边的所有内容
上一个_标记=“”
对于行中的标记。拆分(','):
如果以前的\u令牌:
setdefault(上一个_标记,[])。append(标记)
setdefault(令牌,[]).append(上一个\u令牌)
前一个令牌=令牌
导入pprint
pprint.pprint(邻居)
输出
a,c,f,g,hi,lw:1
f,g,j,ew,f,h,a,w:3
fd,s,f,g,s:4
{'a':['c','h','w'],
'c':['a','f'],
“ew”:['j','f'],
‘f’:[‘c’、‘g’、‘g’、‘ew’、‘h’、‘s’、‘g’],
'fd':['s'],
‘g’:[f’、‘嗨’、‘f’、‘j’、‘f’、‘s’],
'h':['f','a'],
‘嗨’:['g','lw'],
'j':['g','ew'],
“lw”:[“hi”],
's':['fd','f','g'],
'w':['a']}
整理预印好的词典留给读者作为练习。(因为字典本质上不按任何顺序排序,在不改变列表顺序的情况下删除重复项也很烦人)
简易解决方案:
对于word,邻居\邻居中的列表。项()
打印单词“:”,“,”。连接(集合(邻居列表))
但这确实改变了顺序。输入
a,c,f,g,hi,lw:1
f,g,j,ew,f,h,a,w:3
fd,s,f,g,s:4
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
代码
neights={}
对于文件('4-input.txt')中的行:
line=line.strip()
如果不是直线:
继续#跳过空输入行
line=line[:line.index(“:”)]#取“:”左边的所有内容
上一个_标记=“”
对于行中的标记。拆分(','):
如果以前的\u令牌:
setdefault(上一个_标记,[])。append(标记)
setdefault(令牌,[]).append(上一个\u令牌)
前一个令牌=令牌
导入pprint
pprint.pprint(邻居)
输出
a,c,f,g,hi,lw:1
f,g,j,ew,f,h,a,w:3
fd,s,f,g,s:4
{'a':['c','h','w'],
'c':['a','f'],
“ew”:['j','f'],
‘f’:[‘c’、‘g’、‘g’、‘ew’、‘h’、‘s’、‘g’],
'fd':['s'],
‘g’:[f’、‘嗨’、‘f’、‘j’、‘f’、‘s’],
'h':['f','a'],
‘嗨’:['g','lw'],
'j':['g','ew'],
“lw”:[“hi”],
's':['fd','f','g'],
'w':['a']}
整理预印好的词典留给读者作为练习。(因为字典本质上不按任何顺序排序,在不改变列表顺序的情况下删除重复项也很烦人)
简易解决方案:
对于word,邻居\邻居中的列表。项()
打印单词“:”,“,”。连接(集合(邻居列表))
但是