Python 返回唯一的单词词典,其关键字为数字,其值为等长单词的列表
我编写了一个函数n_letter_dictionary(my_string)来获取一个字典,它的键是数字,值是包含唯一单词的列表。在这里,一个单词的字母数等于键。这是我的密码:Python 返回唯一的单词词典,其关键字为数字,其值为等长单词的列表,python,list,dictionary,Python,List,Dictionary,我编写了一个函数n_letter_dictionary(my_string)来获取一个字典,它的键是数字,值是包含唯一单词的列表。在这里,一个单词的字母数等于键。这是我的密码: def n_letter_dictionary(my_string): my_string = my_string.lower().split() L= [] for key in my_string: if len(key) >1: l={len(
def n_letter_dictionary(my_string):
my_string = my_string.lower().split()
L= []
for key in my_string:
if len(key) >1:
l={len(key):[key]}
L.append(l)
L.sort()
return L
s="The way you see people is the way you treat them and the Way you treat them is what they become"
print(n_letter_dictionary(s))
正确的输出应为:
{2: ['is'], 3: ['and', 'see', 'the', 'way', 'you'], 4: ['them', 'they', 'what'], 5: ['treat'], 6: ['become', 'people']}
而我的代码给出了以下输出:
[{2: ['is']}, {2: ['is']}, {3: ['and']}, {3:['see']},{3:['the']},{3:['the']},{3:['the']}, {3:['way']}, {3:['way']},{3:['way']},{3:['you']}, {3:['you']},{3:['you']}, {4:['them']}, {4:['them']}, {4:['they']},{4:['what']},{5: ['treat']},{5: ['treat']},{ 6:['become']}, {6:['people']}]
如何获得正确的输出?您可以使用a,它可以自动为每个条目设置,如下所示。这将确保只存储唯一的条目:
from collections import defaultdict
def n_letter_dictionary(my_string):
words = my_string.lower().split()
lengths = defaultdict(set)
for key in words:
if len(key) > 1:
lengths[len(key)].add(key)
return {key : sorted(value) for key, value in lengths.items()}
s = "The way you see people is the way you treat them and the Way you treat them is what they become"
lengths = n_letter_dictionary(s)
for key in sorted(lengths.keys()):
print(key, lengths[key])
注意,您不能对字典进行排序,但可以在已排序的目录中显示内容:
2['is']
3[“and”,“see”,“the”,“way”,“you']
4[‘他们’、‘他们’、‘什么’]
5[“治疗”]
6[“成为”、“人”]
您正在为遇到的每个键构建一个新词典,并将其附加到列表中
我的建议是使用带有空集的defaultdict
作为默认值,以避免附加重复项(这也可以通过列表来完成,但是使用O(n)成员资格测试,而不是集合的O(1))。排序和列表转换可以在累积所有字长的所有单词后完成
>>> from collections import defaultdict
>>> d = defaultdict(set)
>>>
>>> for word in s.lower().split():
... the_len = len(word)
... if the_len > 1:
... d[the_len].add(word)
...
>>> d = {k:sorted(v) for k,v in d.items()}
>>> d
{2: ['is'], 3: ['and', 'see', 'the', 'way', 'you'], 4: ['them', 'they', 'what'], 5: ['treat'], 6: ['become', 'people']}
如果不想使用defaultdict
,可以使用常规dict
并更改行
d[the_len].add(word)
到
我编写了以下代码,给出了正确的输出:
def n_letter_dictionary(my_string):
from collections import defaultdict
my_string = my_string.lower().split()
L= []
for key in my_string:
if len(key) >=1:
l=(len(key),key)
L.append(l)
d = defaultdict(list)
for k, v in L:
d[k].append(v)
values=d.items()
return_dic={}
for k, v in values:
return_dic[k]=sorted(list(set(v)))
return return_dic
谢谢这里有一个oneliner,请注意,使用此方法可能会出现空列表:
In[12]: {k:list(set(filter(lambda x: len(x) == v, s.split()))) for k,v in enumerate(range(max(map(len, s.split()))))}
Out[12]:
{0: [],
1: [],
2: ['is'],
3: ['and', 'Way', 'you', 'see', 'way', 'the', 'The'],
4: ['them', 'what', 'they'],
5: ['treat']}
记录集合,而不是指示列表。也可以使用collections.defaultdict。正确输出列表中的单词顺序是否重要?这不会产生所需的结果。刚刚发现了这一点,我还假设OP通过查看代码不需要长度为1的单词。@Evans,值列表需要按字母顺序排序。您可以看到I和@timegeb已经发布了更正的代码。您的值列表不是按升序排列的values@ohid如果您需要的话,对列表进行排序非常简单<代码>已排序(v)而不是
列表(v)
In[12]: {k:list(set(filter(lambda x: len(x) == v, s.split()))) for k,v in enumerate(range(max(map(len, s.split()))))}
Out[12]:
{0: [],
1: [],
2: ['is'],
3: ['and', 'Way', 'you', 'see', 'way', 'the', 'The'],
4: ['them', 'what', 'they'],
5: ['treat']}