2016-07-07 51 views
2

我想在用户输入句子的地方制作一些代码,将句子转换为字典,然后使用字典获取原始句子。如何从字典中创建一个句子

代码:

import json 
def code(): 
    sentence = input("Please write a sentence: ") 
    dictionary = {v: k for k,v in enumerate(sentence.split(), start=1)} 
    with open('Dict.txt', 'w') as fp: 
     json.dump(dictionary, fp) 
    print(dictionary) 
    puncList = ["{","}",",",":","'","[","]","1","2","3","4","5"] 
    for i in puncList: 
     for sentence in dictionary: 
      dictionary=[sentence.replace(i," ") for sentence in dictionary] 
    print(' '.join(dictionary)) 
code() 

输入:

Hello my name is Bob 

实际输出:

{'Hello' : '1', 'name' : '3', 'Bob' : '5', 'my' : '2', 'is' : '4'} 
Hello name Bob my is 

希望的输出:

{'Hello' : '1', 'name' : '3', 'Bob' : '5', 'my' : '2', 'is' : '4'} 
Hello my name is Bob 

这将是罚款太:

{'Hello' : '1', 'my' : '2', 'name' : '3', 'is' : '4', 'Bob' : '5'} 
Hello my name is Bob 

对于那些我重新原判的一部分,它不能只是打印了一句,它必须是从字典。

+0

您可以使用OrderedDict –

回答

2

您需要使用OrderedDict来保留元素顺序,或者在打印出字典元素之前排序字典元素。你已经有了一个OrderedDict答案,所以这里是如何使用您创建的字典:

print(' '.join(k for (k, v) in sort(dictionary.items(), key=lambda x: x[1]))) 

顺便说一句,你的方法有一个缺陷:如果你将它应用到一个句子重复的单词,如“男生会是男孩“,你会发现在你的字典里没有索引1的元素,因为(boys, 4)会覆盖(boys, 1)

1

使用OrderedDictenumerate,就像这样:

from collections import OrderedDict 

s = "Hello my name is Bob" 

d = OrderedDict((v, i) for i, v in enumerate(s.split(), 1)) 
print(d) 
# OrderedDict([('Hello', 1), ('my', 2), ('name', 3), ('is', 4), ('Bob', 5)]) 

s_rebuild = ' '.join(d) 
print(s_rebuild) 
# 'Hello my name is Bob' 

由于字典已经下令,值不用于重建的字符串。

1

您逻辑缺陷在于不能重复的单词的句子处理:

Hello Bob my name is Bob too 
{'name': 4, 'Hello': 1, 'Bob': 6, 'is': 5, 'too': 7, 'my': 3} 
name Hello Bob is too my 

我们可以处理,使用一个defaultdict,使得字位置,而不是单个数字的值数组。我们可以通过处理您的打孔清单通过拆分。最后,我们可以使用一对嵌套循环重建原始语句。我们不希望/需要一个OrderedDict或排序,要做到这一点:

import re 
import json 
from collections import defaultdict 

PUNCH_LIST = r"[ {},:'[\]1-5]+" 

def code(): 
    dictionary = defaultdict(list) 

    sentence = input("Please write a sentence: ") 

    for position, word in enumerate(re.split(PUNCH_LIST, sentence), start=1): 
     dictionary[word].append(position) 

    with open('Dict.txt', 'w') as fp: 
     json.dump(dictionary, fp) 

    print(dictionary) 

    position = 1 
    sentence = [] 

    while position: 
     for word, positions in dictionary.items(): 
      if position in positions: 
       sentence.append(word) 
       position += 1 
       break 
     else: 
      position = 0 

    print(' '.join(sentence)) 

code() 

例:

Please write a sentence: Hello Bob, my name is Bob too 
defaultdict(<class 'list'>, {'is': [5], 'too': [7], 'Bob': [2, 6], 'Hello': [1], 'name': [4], 'my': [3]}) 
Hello Bob my name is Bob too 

其中Dict.txt包含:

{"is": [5], "too": [7], "Bob": [2, 6], "Hello": [1], "name": [4], "my": [3]} 

注意defaultdict是一种方便,而不是要求。一个普通的字典会做,但你必须初始化每个键的列表。