简体   繁体   English

使具有共同字符的字符串字典

[英]Make Dictionary of Strings with Common Characters

I'm stuck on a homework problem and am utterly stumped with no ideas left. 我陷入了家庭作业问题,完全陷入困境,没有任何想法。 I have tried different variations of code for the last 7 hours to no avail. 在过去的7个小时里,我尝试了各种代码变化,但无济于事。 The homework question is as follows: 作业问题如下:

Write a function named shareALetter that takes one parameter, wordList -- a list of words. 编写一个名为shareALetter的函数,该函数带有一个参数wordList-单词列表。 Create and return a dictionary in which each word in wordList is a key and the corresponding value is a list of all the words in wordList that share at least one letter with that word. 创建并返回一个字典,其中wordList中的每个单词都是一个键,而对应的值是wordList中与该单词共享至少一个字母的所有单词的列表。 There should be no duplicate words in any value in the dictionary. 字典中的任何值都不应有重复的单词。

For example, the following is correct output: 例如,以下是正确的输出:

print(shareALetter(horton))
{'I': ['I'],
 'say': ['say', 'what', 'mean', 'and'], 
 'what': ['say', 'what', 'mean', 'and'], 
 'mean': ['say', 'what', 'mean', 'and'], 
 'and': ['say', 'what', 'mean', 'and']}

Below is the code that has come the closest to solving the problem: 以下是最接近解决问题的代码:

def shareALetter(wordList):
    shareCount = {}
    words=[]
    string=''
    for word in wordList:
        if word not in words:
            words.append(word)
        if word not in string:
            string += word
        if word not in shareCount:
            shareCount[word] = ''
    for key in shareCount:
        sharedWords = []
        for word in words:
            for letter in string:
                if letter in word and word not in sharedWords:
                    sharedWords.append(word)
                if word not in shareCount:
                    shareCount[word]=sharedWords

    return(shareCount)
print(shareALetter(test1))

I know this is terrible, sloppy, and inefficient with probably over a dozen issues, but I can't figure out how to fix any of the problems here. 我知道这很糟糕,草率而且效率低下,可能有十几个问题,但是我不知道如何解决这里的任何问题。 Any help would be appreciated. 任何帮助,将不胜感激。

When you need to create this kind of algorithm, it's helpful to imagine how you would approach the situation with just pen and paper. 当您需要创建这种算法时,想象一下如何用笔和纸来处理这种情况会很有帮助。 If I were you, I'd actually write out a few words and complete the exercise manually. 如果我是你,我实际上会写下几句话,然后手动完成练习。 As you do so, note how you are working through the problem, and see if you can translate that series of actions into code. 在执行此操作时,请注意如何解决问题,并查看是否可以将一系列操作转换为代码。

Here's a bit of a nudge to keep you going: 为了让您继续前进,这里有一些提示:

def share_a_letter(word_list):
  character_matches = {}
  for word_one in word_list:
    character_matches[word_one] = set()
    for word_two in word_list:
      for character in word_two:
        # TODO:
        # check if the character is in word_one
        # if so, do something special
  return character_matches

words = ['hello', 'wopper', 'cat', 'pickle']
print(share_a_letter(words))

确保添加正确的变量,这是我的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM