检查字典中的唯一值并返回列表 [英] Check for unique values in a dictionary and return a list

查看:120
本文介绍了检查字典中的唯一值并返回列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力这个练习几天,我发现每一个近似值都有一个新的问题,这个想法是在字典上找到这些独特的值,并返回一个带有键的列表



例如:
如果 aDictionary = {1:1,3:2,6:0,7:0,8:4,10 :0} 那么你的函数应该返回 [1,3,8] ,因为1,2和4的值只能出现一次。 / p>

这是我迄今为止所尝试的:

  def existsOnce (aDict):

counting = {}
tempList = []

在aDict.keys()中的k:
print k,
打印aDict [k]


打印'值为:'
for a inDict.values():
print v,
counting [v] = counting.get(v,0)+1
打印计数[v]
tempNumbers = counting [v]
tempList.append(tempNumbers)
print tempList

如果我这样走,我可以指出和删除那些大于一个的那些,但是问题依然存在,我会有一个零,我不希望它在原始列表中不是唯一的。

  def existsOnce2(aDict):

#import顶部有from collections import Counter的计数器模块

c = Counter()

在这里是英文文本的样本:
c [letter] + = 1
如果c [letter] == 1:
print c [letter],':',letter

我尝试以这种方式使用整数,并检查哪些出现在第一次,但不能将其转换为字典或继续从这里。另外我不知道导入模块是否被允许在答案中,肯定是一种没有外部模块的方法。

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

'''直到这里我得到一个值在原来的数值出现的次数字典,现在我应该删除那些大于1'''
temp_vals = vals.copy()
for val中的x:
如果vals [x]> 1:
print'delete this:','key:',x,'value:',vals [x]
temp_vals.pop(x)
else:
pass
打印'临时字典值:',temp_vals
'''直到这里我减少出现一次,1,2和4的值,现在我需要返回并检查原始字典和返回键
原始字典:{1:1,3:2,6:0,7:0,8:4,10:0}
temp_vals {'1':1,'2' :1,'4':1} temp_vals(1,2,4)中的
键与我从原始字典中检索到的键相关联的值(1,3,8)
'' '
print'---'

temp_list = []
在temp_vals中的每个TempVal:
temp_list.append(eachTempVal)
print'临时列表值:',temp_list
'''直到这里我得到一个临时列表,我需要在aDict'''
print'---'
for eac hListVal in temp_list:
print'eachListVal:',eachListVal
for k,v in aDict.iteritems():
print'key:',k,'value:',v

从这里我无法以任何原因获取值,并比较它们,我试图提取值如下:

 如果v == eachListVal:
做某事

但我做错了,无法访问这些值。

解决方案

您只需要使用 vals dict,并从 aDict 中保存键,一个 count == 1 在vals然后调用排序以获得排序的输出列表:

  def existsOnce3(aDict):
vals = {}
#创建dict来计算所有值计数
在iDict.values()中:
vals.setdefault (i,0)
vals [i] + = 1
#使用来自aDict的每个v / val如果计数为1,则$ val
#保留每个k / key的值,如果count为1
返回排序(k表示k,v在aDict.items()中如果vals [v] == 1)

使用collections.Counter dict来计数只需调用Counter就可以应用相同的逻辑从计数器中保留每个k的数值== 1从

 从集合导入计数器
cn =计数器(aDict.values())
打印(排序(k为k,v在aDict.items()中如果cn [v] == 1))


I've been struggling with this exercise for a couple of days now, each approximation I find, have a new problem, the idea is to find those unique values on a dictionary, and return a list with the keys

For example: if aDictionary = {1: 1, 3: 2, 6: 0, 7: 0, 8: 4, 10: 0} then your the function should return [1, 3, 8], as the values 1,2 and 4 only appear once.

This is what I've tried so far:

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

If I go this way, I can point and delete those that are bigger than one, but the problem persists, I will have one zero, and I don't want it as was not unique in the original list.

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

I tried to go this way with integers and check which ones appear from first time, but cannot translate it to dictionary or keep going from here. Also I'm not sure if importing modules are allowed in the answer and surely have to be a way to do it without external modules.

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

From here I cannot take the values for whatever reason and compare them, I've tried to extract the values with statements like:

if v == eachListVal:
    do something

But I'm doing something wrong and cannot access to the values.

解决方案

You just need to use your vals dict and keep keys from aDict with values that have a count == 1 in vals then calling sorted to get a sorted output list:

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)

Using a collections.Counter dict to do the counting just call Counter on your values then apply the same logic, just keep each k that has a v count == 1 from the Counter dict:

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

这篇关于检查字典中的唯一值并返回列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆