返回一个唯一的单词字典,其键是数字,其值是相等长度的单词列表

时间:2016-03-14 06:27:14

标签: 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(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']}]

我怎样才能得到正确的输出?

4 个答案:

答案 0 :(得分:1)

您可以使用defaultdict来自动为每个条目设置集合,如下所示。这将确保仅存储唯一条目:

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 ['them', 'they', 'what']
5 ['treat']
6 ['become', 'people']

答案 1 :(得分:0)

您正在为遇到的每个键构建一个新词典,并将其附加到列表中。

我的建议是使用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)

d.setdefault(the_len, set()).add(word)

答案 2 :(得分:0)

我制作了以下代码,它提供了正确的输出:

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

由于

答案 3 :(得分:0)

在这里你有一个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']}