2015-08-09 132 views
2

我一直在爲這個練習掙扎幾天,現在我發現每個近似值都有一個新問題,其思想是在字典上找到這些唯一值,並返回一個列表的鑰匙檢查字典中的唯一值並返回一個列表

例如: 如果aDictionary = {1: 1, 3: 2, 6: 0, 7: 0, 8: 4, 10: 0}那麼你的函數應該返回[1, 3, 8],作爲值1,2和4只出現一次。

這是我到目前爲止已經試過:

def existsOnce(aDict): 

counting = {} 
tempList = [] 

for k in aDict.keys(): 
    print k, 
    print aDict[k] 


print 'values are:' 
for v in aDict.values(): 
    print v, 
    counting[v] = counting.get(v,0)+1 
    print counting[v] 
    tempNumbers = counting[v] 
    tempList.append(tempNumbers) 
print tempList 

如果我走這條路,我可以指出,並刪除那些比一個大,但問題仍然存在,我將有一個零,我不想要它,因爲在原始列表中不是唯一的。

def existsOnce2(aDict): 

# import Counter module in the top with `from collections import Counter` 

c = Counter() 

for letter in 'here is a sample of english text': 
    c[letter] += 1 
    if c[letter] == 1: 
     print c[letter],':',letter 

我試圖用整數去檢查哪些是從第一次出現,但不能將它翻譯成字典或繼續從這裏出發。此外,我不確定導入模塊是否允許在答案中,並且肯定必須是一種無需外部模塊的方式。

def existsOnce3(aDict): 

    vals = {} 
    for i in aDict.values(): 
     for j in set(str(i)): 
      vals[j] = 1+ vals.get(j,0) 
    print vals 

    '''till here I get a counter of how many times a value appears in the original dictionary, now I should delete those bigger than 1''' 
    temp_vals = vals.copy() 
    for x in vals: 
     if vals[x] > 1: 
      print 'delete this: ', 'key:',x,'value:', vals[x] 
      temp_vals.pop(x) 
     else: 
      pass 
    print 'temporary dictionary values:', temp_vals 
    '''till here I reduced down the values that appear once, 1, 2 and 4, now I would need the go back and check the original dictionary and return the keys 
     Original dictionary: {1: 1, 3: 2, 6: 0, 7: 0, 8: 4, 10: 0} 
     temp_vals {'1': 1, '2': 1, '4': 1} 
     keys on temp_vals (1,2,4) are the values associated to the keys I got to retrieve from original dictionary (1,3,8) 
    ''' 
    print '---' 

    temp_list = [] 
    for eachTempVal in temp_vals: 
     temp_list.append(eachTempVal) 
    print 'temporary list values:', temp_list 
    ''' till here I got a temporary list with the values I need to search in aDict''' 
    print '---' 
    for eachListVal in temp_list: 
     print 'eachListVal:', eachListVal 
     for k,v in aDict.iteritems(): 
      print 'key:',k,'value:',v 

在這裏,我可以不承擔任何原因的值並加以比較,我已經試過類似語句來提取值:

if v == eachListVal: 
    do something 

但我做錯了什麼和不可以訪問到價值。

+0

如果值是不可變的創建通過在第一,其中在所述第二字典的值的計數的值鍵控第二字典它們出現在第一個字典中的次數。從那裏一行代碼返回你想要的列表。它應該全部適合最多6行代碼。 –

回答

3

你只需要使用您的vals字典,並保持鍵從aDict與有丘壑一個count == 1然後調用排序得到有序輸出列表值:

def existsOnce3(aDict): 
    vals = {} 
    # create dict to sum all value counts 
    for i in aDict.values(): 
     vals.setdefault(i,0) 
     vals[i] += 1 
    # use each v/val from aDict as the key to vals 
    # keeping each k/key from aDict if the count is 1 
    return sorted(k for k, v in aDict.items() if vals[v] == 1) 

使用collections.Counter字典做計數只是調用計數器上的值,然後套用同樣的邏輯,只要保持有AV計數== 1從櫃檯字典每個k:

from collections import Counter 
cn = Counter(aDict.values()) 
print(sorted(k for k,v in aDict.items() if cn[v] == 1)) 
+0

太棒了!我仍然不完全明白這一行:'如果vals [v] == 1)'返回排序(k代表k,v代表aDict.items())。任何有用的鏈接到文檔? – gma992

+1

@ gma992,它只是將鍵從最低到最高排序,以保證您獲得所需的順序,字典無序,所以不保證您在不調用排序的情況下得到'[1,3,8]''。 https://wiki.python.org/moin/HowTo/Sorting如果在val中返回'v'的值,'if vals [v] == 1'確保我們只保留其值不重複的鍵字典不是'1',那麼我們知道它不是一個唯一的值 –

1

如何:

from collections import Counter 

my_dict = {1: 1, 3: 2, 6: 0, 7: 0, 8: 4, 10: 0} 

val_counter = Counter(my_dict.itervalues()) 
my_list = [k for k, v in my_dict.iteritems() if val_counter[v] == 1] 

print my_list 

結果:

[1, 3, 8] 
+0

'[k for k,v in my_dict。items()如果val_counter [v] == 1]',不需要先創建'uniques' – yangjie

+0

@yangjie,是的,你是對的。 – Akavall

0

一個襯裏:

>>> aDictionary = {1: 1, 3: 2, 6: 0, 7: 0, 8: 4, 10: 0} 
>>> unique_values = [k for k,v in aDictionary.items() if list(aDictionary.values()).count(v)==1] 
>>> unique_values 
[1, 3, 8] 
+1

這是使用OP的字典方法與O(n)相反的二次方 –

相關問題