简体   繁体   English

您如何为 function 编写名为 makeWordLengthDict 的程序,该程序将单词列表作为其唯一参数,并在 python 中返回字典

[英]how do you write a program for function named makeWordLengthDict which takes a LIST of words as its only parameter, and returns a dictionary in python

I was trying to write the code for function named makeWordLengthDict which takes a LIST of words as its only parameter, and returns a dictionary that maps each word to the number of characters in that word.我试图为 function 编写名为 makeWordLengthDict 的代码,该代码将单词列表作为其唯一参数,并返回一个字典,该字典将每个单词映射到该单词中的字符数。 can anyone help me how to start it with?谁能帮助我如何开始?

A simple dictionary comprehension would do this:一个简单的字典理解可以做到这一点:

def makeWordLengthDict(list_of_words):
    return {i:len(i) for i in list_of_words}

If the function is called passing ["ball","cat"] as the argument then the function will return: {'ball': 4, 'cat': 3}如果 function 被称为传递["ball","cat"]作为参数,那么 function 将返回: {'ball': 4, 'cat': 3}

I thought I should post the second code before you ask.我想我应该在你问之前发布第二个代码。 So here you go buddy...所以在这里你 go 哥们...

words_list = ["Apple", "Banana", "Ball", "Cat", "Bat", "Rat"] # Only Rat will be update in the Dictionary

def makeWordLengthDict(words):
    keys = [len(word) for word in words]
    words = [word for word in words]

    return(keys, words)

keys, words = makeWordLengthDict(words_list)

# To access them accordingly.
for i in range(len(words_list)):
    print(f"{keys[i]}: {words[i]}")

The code works but there is a problem with this concept.该代码有效,但这个概念存在问题。 If you have two words of the same length, only one of them will be updated to the dictionary(The last one only).如果您有两个相同长度的单词,则只有其中一个会更新到字典中(仅最后一个)。 Because a dictionary can't have more than one item with the same key.因为字典不能有多个具有相同键的项目。 Running the code I've provided will show you the problem.运行我提供的代码将向您显示问题。 But you can do one thing if you find a length that is repeated, you can name them as 3a for another string in the list with 3 characters.但是,如果您发现一个重复的长度,您可以做一件事,您可以将它们命名为 3a 用于列表中具有 3 个字符的另一个字符串。 But this might sound a bit inappropriate, so you can use two lists.但这听起来可能有点不合适,因此您可以使用两个列表。 One will act as the key of the dictionary and the other one as the value for that key.一个将作为字典的键,另一个作为该键的值。 Reply "Second Code" if you want the second one.如果您想要第二个代码,请回复“第二个代码”。

words_list = ["Apple", "Banana", "Ball", "Cat", "Bat", "Rat"] # Only Rat will be update in the Dictionary

def makeWordLengthDict(words):
    word_dict = {}
    for word in words:
        length = len(word)
        word_dict.update({length: word})

    return(word_dict)

print(makeWordLengthDict(words_list))

暂无
暂无

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

相关问题 我如何编写一个程序,将字符串列表作为输入并返回一个字典,其中包含匹配字符串的单词索引 - how do I write a program that takes a list of strings as input and returns a dictionary, containing an index of the words to the matching strings 如何在 Python 3.10 中创建一个接受参数并返回字符计数字典的 function? - How do you create a function that takes an argument and returns a dictionary of character counts in Python 3.10? 您如何在Python中编写一个函数,该函数需要一个字符串并返回一个新字符串,该字符串是原始字符串,并且重复了所有字符? - How do you write a function in Python that takes a string and returns a new string that is the original string with all of the characters repeated? 编写一个 function,它以单词列表作为参数,并将每个单词加倍 - Write a function that takes a list of words as a parameter and that double every word 如何将 python function 编写为返回字典类型的 udf - How to write a python function as udf which returns a dictionary type 如何编写返回字典的 python function - How do I write a python function that returns a dictionary 如何编写一个 function 获取名称列表并将其转换为字典 - How to write a function that takes a list of names and turns it into a dictionary 编写一个函数 filter_long_words() 接受一个单词列表和一个整数 n 并返回长度大于 n 的单词列表 - Write a function filter_long_words() that takes a list of words and an integer n and returns the list of words that are longer than n 如何在 python 中制作 function ,它将整数列表作为输入并输出只有两个值的较小列表? - How do I make a function in python which takes a list of integers as an input and outputs smaller lists with only two values? 如何编写一个 function 占用一行并返回二维元组列表 - How do I write a function that takes one row and returns a list of 2-dimension tuples
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM