Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

python - 与字典斗争(Struggling with dictionaries)

I would appreciate it if somebody could help me solve the following exercise.

(如果有人可以帮助我解决以下问题,我将不胜感激。)

Write a function that takes a string containing words separated by spaces and creates a dictionary where the keys correspond to the last characters of the words of the string, whilst the associated values correspond to the lists having the words ending in that character.

(编写一个函数,该函数接受包含用空格分隔的单词的字符串,并创建一个字典,其中的键对应于字符串中单词的最后一个字符,而关联的值对应于具有以该字符结尾的单词的列表。)

Furthermore, the lists must be lexicographically sorted

(此外,列表必须按字典顺序排序)

Example

(例)

string = "This will be an amazing experience"

Should return the following dictionary

(应该返回以下字典)

{'s':['This'], 'l':['will'], 'e':['be', 'experience'], 'n':['an'], 'g':['amazing']}

I have an idea of how the exercise should be solved.

(我对如何解决这项运动有一个想法。)

However, given that i am still a beginner in Python, i struggle.

(但是,鉴于我仍然是Python的初学者,所以我很努力。)

The first thing i did is create an empty dictionary.

(我做的第一件事是创建一个空字典。)

Secondly, I converted the string of words into a list using the split method.

(其次,我使用split方法将单词字符串转换为列表。)

Afterwards, i created another list that takes the last character of the words of the first list of strings and puts it into the new list.

(之后,我创建了另一个列表,该列表采用第一个字符串列表中单词的最后一个字符,并将其放入新列表中。)

I used dict in order to put the elements of the second list as keys in my dictionary which works quite well.

(我使用dict以便将第二个列表的元素作为字典中的键,效果很好。)

The problem is i cannot put the list of corresponding words as values.

(问题是我不能将相应单词的列表作为值。)

Here is my code:

(这是我的代码:)

def dictionary (string):

    mydict = {}
    mylist = string.split()
    mylist2 = []

    for word in mylist:

        mylist2.append(word[-1])
        mydict = dict((el,(word[-1] for word[-1] in mylist)) for el in mylist2)

    return mydict
  ask by alex108 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need two lists, the algorithm is pretty simple:

(您不需要两个列表,该算法非常简单:)

  1. initialise an empty dict

    (初始化一个空的字典)

  2. split the sentence into words

    (把句子分成单词)

  3. loop through the words

    (遍历单词)

  4. get the last character of the word, this is the key for the dict

    (得到单词的最后一个字符,这是字典的关键)

  5. check if that key already exists in the dict, and if not, initialise it with an empty list

    (检查字典中是否已存在该键,如果不存在,请使用一个空列表对其进行初始化)

  6. append the word to said list

    (将单词附加到列表中)

result = {}

for word in string.split():
    key = word[-1]

    if key not in result:
        result[key] = []

    result[key].append(word)

There are ways to shorten that using dict.setdefault or defaultdict , but this is the basic idea.

(有多种方法可以使用dict.setdefaultdefaultdict来缩短它,但这是基本思想。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...