2017-08-11 38 views
-2

我有一个字符串来分类吧...如何把我的字典值类{}改为[]和键

my_string="The way you see people is the way you treat them and the Way you treat them is what they become" 

我DEF应该返回此:

{2: ['is'], 
3: ['and', 'see', 'the', 'way', 'you'], 
4: ['them', 'they', 'what'], 
5: ['treat'], 
6: ['become', 'people']} 

我的解决方案回报:

{3: {'you', 'see', 'way', 'and', 'the'}, 
6: {'become', 'people'}, 
2: {'is'}, 
5: {'treat'}, 
4: {'what', 'them', 'they'}} 

我需要通过排序键和变化值的类字典...我的价值类是{},但我想[] 我解决方案:

def n_letter_dictionary(my_string): 
    my_string=my_string.lower().split() 
    sample_dictionary={} 
    r=[] 
    for word in my_string: 
     lw=len(word) 
     if lw in sample_dictionary: 
      sample_dictionary[lw].add(word) 
     else: 
      sample_dictionary[lw] = {word} 

    return sample_dictionary 



print(n_letter_dictionary("The way you see people is the way you treat them 
and the Way you treat them is what they become")) 

我怎么能做到这一点任何人都可以帮助吗?

+0

字典无法排序。它们是非结构化数据容器(至少达到Python 3.6)。将'set'转换为'list'很简单 –

+0

['collections.OrderedDict'](https://docs.python.org/3/library/collections.html#collections.OrderedDict)会返回右边的项目订单,但我不确定它将如何打印。你还需要像'return OrderedDict(sorted(sample_dictionary.items(),key = lambda x:x [0]))'' – Kendas

回答

1

你有套,因为你在这里创建一个:

sample_dictionary[lw] = {word} 

你需要使它成为一个名单有:

sample_dictionary[lw] = [word] 

,并使用.append(),不.add()加入更多的元素。

请注意,你的代码可以使用dict.setdefault()被简化:

def n_letter_dictionary(my_string): 
    sample_dictionary = {} 
    for word in my_string.lower().split(): 
     sample_dictionary.set_default(len(word), []).append(word) 
    return sample_dictionary 

.setdefault()返回给定键的值;如果缺少密钥,则首先将该密钥设置为第二个参数中提供的默认值。

如果你想只保留独特话,你就必须要么转换集来列出事实后一个额外的循环:

def n_letter_dictionary(my_string): 
    sample_dictionary = {} 
    for word in my_string.lower().split(): 
     sample_dictionary.set_default(len(word), set()).add(word) 
    return {l: list(v) for l, v in sample_dictionary.items()} 

最后一行是一本字典的理解;它使用相同的密钥构建一个新的字典,并将每个值转换为一个列表。请注意,集合是无序的,所以结果列表将以任意顺序列出唯一字词。如果您需要保留输入中单词的顺序,那么您必须将这些单词收集到列表中,然后将How do you remove duplicates from a list in whilst preserving order?中的技术应用于每个值。

词典在其他方面也是无序的,就像集合一样,不能排序。有关变通的信息,请参见How can I sort a dictionary by key?

例如,你可以从生产排序(key, value)双的OrderedDict() instance

from collections import OrderedDict 

def n_letter_dictionary(my_string): 
    sample_dictionary = {} 
    for word in my_string.lower().split(): 
     sample_dictionary.set_default(len(word), set()).add(word) 
    return OrderedDict((l, list(v)) for l, v in sorted(sample_dictionary.items())) 
0

日文N3 N4 N5中默认蟒蛇无序。你可以做的是使用OrderedDict。它保留了数据插入的顺序,并且如果插入了已排序的数据,它将保持排序状态。

from collections import OrderedDict 
unordered_dict = { 
    3: {'you', 'see', 'way', 'and', 'the'}, 
    6: {'become', 'people'}, 
    2: {'is'}, 
    5: {'treat'}, 
    4: {'what', 'them', 'they'}} 

ordered_dict = OrderedDict() 
for key in sorted(unordered_dict.keys()): 
    ordered_dict[key] = unordered_dict[key] 
+1

你不需要调用'keys )'产生一个可迭代的键。 'sorted(unordered_dict)'会做。是的,我忘记了那部分。接得好。 –

0

您还可以使用集合中的Counter()来解决此问题。它会让你的生活更轻松。

import collections 
c = collections.Counter(mystring.lower().split(' ')) 
for key in sorted([*c]): 
    print("{0} : {1}".format(key, c[key]))