[英]Python - iterate through an entire list and assign it to a dict
text = "This is a test for my program"
new_dict = {}
text_list = text.split()
word_tester = 2
for word in text_list:
word_tester = len(word)
if len(word) == word_tester:
new_dict[word_tester] = word
return new_dict
I am trying to build a program in python that goes through a list of strings and assigns them to a dict where the key is the amount of characters in that string and the value is the word itself (eg: 2 : be, to 3 : foo, bar). 我正在尝试在python中构建一个程序,该程序遍历字符串列表并将其分配给dict,其中键是该字符串中的字符数,值是单词本身(例如:2:be,to 3: foo,bar)。 My program however only goes through and assigns some of the given list of strings.
但是,我的程序只会遍历并分配一些给定的字符串列表。 What can I do to get this to work?
我该怎么做才能使它正常工作?
I think you just need to be sure to split on the space. 我认为您只需要确保在空间上分开即可。 I ran this, and it works.
我运行了它,并且有效。
text = "This is a test for my program"
text_list = text.split(" ")
new_dict = {}
for word in text_list:
if len(word) in new_dict and word not in new_dict[len(word)]:
new_dict[len(word)].append(word)
else:
new_dict[len(word)] = [word]
#print(new_dict)
#{1: ['a'], 2: ['is', 'my'], 3: ['for'], 4: ['This', 'test'], 7: ['program']}
return new_dict
#1
text = "This is a test for my program"
final_dct = {len(word):word for word in text.split() if len(word)==len(word_tester)}
#2
text = "This is a test for my program"
new_dict = {}
text_list = text.split()
for word in text_list:
if len(word) in new_dict and word not in new_dict[len(word)]:
new_dict[len(word)].append(word)
else:
new_dict[len(word)] = [word]
return new_dict
I think it would be as simple as: 我认为这很简单:
for word in text_list:
new_dict[len(word)] = word
Note that in this dictionary, the key 4 is assigned to test, not this, because only one value can be assigned per key. 请注意,在此字典中,键4是分配给测试的,而不是分配给test的,因为每个键只能分配一个值。 To make the value a list in this case, you could use:
要在这种情况下将值设为列表,可以使用:
for word in text_list:
if len(word) in new_dict:
new_dict[len(word)].append(word)
else:
new_dict[len(word)] = [word]
The problem is that a key can only have one value so you overwrite it every time you have a word with a given length. 问题在于,键只能有一个值,因此,每次有给定长度的单词时,您都将其覆盖。 To remedy this you could store a list of strings as the dictionary value instead of a single string, as suggested in another example.
为了解决这个问题,您可以将字符串列表存储为字典值,而不是单个字符串,如另一个示例所示。
One handy tool from the collections
module is the defaultdict
, which lets you define a default entry if a key doesn't already have a value. collections
模块中的一个便捷工具是defaultdict
,如果键没有值,则可以使用它定义默认条目。 A common use case is to start keys out with an empty list using defaultdict(list)
. 一个常见的用例是使用
defaultdict(list)
从一个空列表开始密钥。 This saves you from having to check if a key doesn't exist yet and initializing it to an empty list manually. 这使您不必检查密钥是否不存在,而无需手动将其初始化为空列表。 Incorporating this into your example, you would get:
将其合并到您的示例中,您将获得:
from collections import defaultdict
text = "This is a test for my program"
new_dict = defaultdict(list) # a dictionary whose default value is an empty list
text_list = text.split()
for word in text_list:
# append this word to the list of words with the same length
new_dict[len(word)].append(word)
return new_dict
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.