2017-08-11 17 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]))