Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从单词列表中不带空格的字符串中获取多个子字符串?_Python_List_Dictionary_Encoding_While Loop - Fatal编程技术网

Python 如何从单词列表中不带空格的字符串中获取多个子字符串?

Python 如何从单词列表中不带空格的字符串中获取多个子字符串?,python,list,dictionary,encoding,while-loop,Python,List,Dictionary,Encoding,While Loop,我想“解码”一条编码信息。每个字母都用唯一的5/6/7/8长“单词”编码。我有一本这些代码的字典('a':'qwert',…)。我试着像这样解码信息: #示例字典 d={'a':'00101','b':'10001011','c':'01100','d':'1111110','e':'01001','f':'010000','g':'1100100','h':'00010010','i':'0000000','j':'1101010','k':'110001','l':'101010','m':

我想“解码”一条编码信息。每个字母都用唯一的5/6/7/8长“单词”编码。我有一本这些代码的字典('a':'qwert',…)。我试着像这样解码信息:

#示例字典
d={'a':'00101','b':'10001011','c':'01100','d':'1111110','e':'01001','f':'010000','g':'1100100','h':'00010010','i':'0000000','j':'1101010','k':'110001','l':'101010','m':'10010','n':'100001','o':'111101','p':'11100111','q':'011110','r':'010001','s':'1110010','t','1110010','u':'11100','11100','101、‘x’:‘011111’、‘y’:‘0111100’、‘z’:‘0111000’、‘11101011’、‘00111101’、‘11111’、‘11111’、‘000100’、‘01101111’、‘10111’、‘10110’、‘10110110’、‘00000101’、‘0’、‘10001’、‘1’、‘000101’、‘2’、‘101011’、‘3’、‘11011001’、‘4’、‘10010111’、‘1015’、‘1016’、‘10110110110’、‘01010’、‘0019’、‘00110’、‘10110’、‘10110’、‘10110’、‘10110’、‘10110’、‘1018’、‘10110’、‘10110’、‘10110’、‘10111’、‘10111’、‘10111’、‘101
#编码字
coded='000100100100101010101010111101'#coded和d是输入,'hello'是预期输出
def get_键(v):
对于键,d.items()中的值:
如果v==值:
返回键
一无所获
def解码(文本):
l=[]
而len(text)>0:
对于范围(5,9)内的i:
如果get_键(文本[0:i])!=无:
l、 追加(获取_键(文本[0:i]))
text=text[i:]
返回“”。加入(str(i)表示l中的i)
逆={v:k代表k,v代表d.items()}
def解码(文本):
l=[]
而len(text)>0:
对于范围(5,9)内的i:
如果文本[0:i]==inverse.keys():
l、 追加(inverse.value())
text=text[i:]
返回“”。加入(str(i)表示l中的i)

但我的代码不起作用。运行此代码需要相当长的时间,我认为它也会返回错误的字母。我不确定如何更正它。您能帮我修复我的代码吗?

我与您的做法不同,这里有大量的优化空间。关键是此函数将返回ind列表子字符串的EXE:

# function to get all the indexes of a word in a string of words
def all_word_indexes(string, substring):
    x = [len(i) for i in string.split(substring)[:-1]]
    return [sum(x[:i+1]) + i*len(substring) for i in range(len(x))]
其余部分非常简单,如下所示:

  • 从单词词典中获取单词列表
  • 遍历列表,查找每个示例索引
  • 在“find”列表中迭代,创建一个元组列表,其中包含单词索引和实际单词
  • 按索引编号对这些字母进行排序,因为它们在原始词典中是最新的,以便获得字母的正确顺序
  • 迭代此列表,将其与反向键、值->值、键字典相匹配
在这里:


你能编辑问题并添加输入和预期输出吗?你能提供输入和预期输出的示例吗?你的
get_键对每次查找都会对字典进行线性扫描。一次全部反转字典会更有效(例如
inverse={v:k代表k,v代表d.items()}
)而不是重复这样做。另外--不要给字典命名,因为这是内置的名字。现在你正在对整个字典中的每一个字母进行迭代。你应该先写一些代码来反转你的字典,这样你就可以对每个字母进行单次查找。@johncleman谢谢你的帮助!你是认真的吗这样吗?(我编辑了我的问题)我不确定它是否正确。谢谢!问题是我有一个类似“RecipePaienceLeadershipMonthBanana…”的代码信息,所以我不知道每个单词的结尾。这对你有用吗?谢谢你的帮助!不幸的是,不,我认为问题是,在我的情况下,字典中有一些值是在彼此之后编写的,可以使增加一个新值。例如:“surfaces”+“unnatural”->“sun”。我编辑了我的问题。这不是为了家庭作业,而是为了我自己。我对解决方案感兴趣。我想你误解了Stackoverflow的目的。它不是让人们将工作发布到其中,并期望人们为你做。上面列出的代码按照你在y中要求的功能运行我们的原始问题和应已标记为正确。如果您有进一步的问题,您应提问并在其中引用此问题,而不是编辑该问题,以便寻找答案的用户看到不同的内容。
secret_dict = {
    'a':'nation',
    'b':'variation',
    'c':'investment',
    'd':'exam',
    'e':'patience',
    'f':'inspection',
    'g':'significance',
    'h':'recipe',
    'i':'consequence',
    'j':'speaker',
    'k':'historian',
    'l':'leadership',
    'm':'meaning',
    'n':'marriage',
    'o':'month',
    'p':'loss',
    'q':'volume',
    'r':'environment',
    's':'cheek',
    't':'database',
    'u':'country',
    'v':'teacher',
    'w':'bonus',
    'x':'football',
    'y':'grocery',
    'z':'income',
    ' ':'banana'
}

# create words list from the the values of the dictionary
my_words_list = list(secret_dict.values())

# switch the values to keys and vice versa
secret_dict_reversed = dict((v,k) for k,v in secret_dict.items()) # reverse the dictionary to be value -> key

# coded phrase is hello world
coded_word = ['recipe', 'patience', 'leadership', 'leadership', 'month', 'banana', 'bonus', 'month', 'environment', 'leadership', 'exam'] # list of words, if you have a string then split it with mystring.split()
decoded_word = ''

coded_string = 'recipepatienceleadershipleadershipmonthbananabonusmonthenvironmentleadershipexam'

results = []

# function to get all the indexes of a word in a string of words
def all_word_indexes(string, substring):
    x = [len(i) for i in string.split(substring)[:-1]]
    return [sum(x[:i+1]) + i*len(substring) for i in range(len(x))]

# iterate through each word in the list
for word in my_words_list:
    word_indexes = all_word_indexes(coded_string, word)
    if len(word_indexes) > 0:
        for idx in word_indexes:
            # returns a list of tuples with word and their index or indexes
            results.append((idx, word))

# sort these results
sorted_results = list(sorted(results, key=lambda elem: elem[0]))
sorted_word_results = [x[1] for x in sorted_results]

# traverse the words to get the letters
for word in sorted_word_results:
    letter = secret_dict_reversed[word]
    decoded_word = decoded_word + letter

print(decoded_word)
# hello world