Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将字典的值分组在一起_Python - Fatal编程技术网

在Python中将字典的值分组在一起

在Python中将字典的值分组在一起,python,Python,我有一本字典如下: comb_dict = ['1d812hjbsa' : ['Apple', 'iPad'], '190usdnb1' : ['Amazon', 'Kindle'], 'sjdb1892': ['Apple', 'iPad'], '23ub8wh12' : ['Apple', 'iPhone'], '12ndsi01' : ['Amazon', 'Kindle'] ] fin_dict= [ ['Apple', 'iPad'] : 2, '['Amazon', 'Kindl

我有一本字典如下:

comb_dict = 
['1d812hjbsa' : ['Apple', 'iPad'], '190usdnb1' : ['Amazon', 'Kindle'], 'sjdb1892': ['Apple', 'iPad'], '23ub8wh12' : ['Apple', 'iPhone'], '12ndsi01' : ['Amazon', 'Kindle'] ]
fin_dict=
[ ['Apple', 'iPad'] : 2, '['Amazon', 'Kindle'] : 2, ['Apple', 'iPhone', : 1 ]
密钥是事务id,值是公司和产品名称。我试着将公司和产品组合在一起,进行计数。对于本例,我希望得到如下输出:

comb_dict = 
['1d812hjbsa' : ['Apple', 'iPad'], '190usdnb1' : ['Amazon', 'Kindle'], 'sjdb1892': ['Apple', 'iPad'], '23ub8wh12' : ['Apple', 'iPhone'], '12ndsi01' : ['Amazon', 'Kindle'] ]
fin_dict=
[ ['Apple', 'iPad'] : 2, '['Amazon', 'Kindle'] : 2, ['Apple', 'iPhone', : 1 ]
我试图将数据读入一个新字典,其键值与输入值相反。但是,它没有像我预期的那样工作,并抛出以下错误:

for key, value in comb_dict.items():
    if fin_dict.has_key(value):
        fin_dict[value] +=1
    else:
        fin_dict.update({value : 1})  
输出

   if fin_dict.has_key(value):
TypeError: unhashable type: 'list'
有人能指出如何解决这个问题吗

谢谢,
TM

错误消息告诉您出了什么问题:不能将列表用作字典键,因为列表是不可散列的

幸运的是,您可以将其转换为可散列的元组。请注意,
collections.Counter
中有一个很好的类,它已经为您进行了计数

import collections
fin = collections.Counter(tuple(i) for i in comb.values())

错误消息告诉您出了什么问题:不能将列表用作字典键,因为列表是不可散列的

幸运的是,您可以将其转换为可散列的元组。请注意,
collections.Counter
中有一个很好的类,它已经为您进行了计数

import collections
fin = collections.Counter(tuple(i) for i in comb.values())

您的字典键不能是列表,因为列表是可变的。您需要一个元组或其他不可变对象

我建议你改为使用计数器

>>> from collections import Counter
>>> dic = {'1d812hjbsa' : ['Apple', 'iPad'], '190usdnb1' : ['Amazon', 'Kindle'], 'sjdb1892': ['Apple', 'iPad'], '23ub8wh12' : ['Apple', 'iPhone'], '12ndsi01' : ['Amazon', 'Kindle']}
>>> counter = Counter(tuple(v) for v in dic.values())
>>> dict(counter)
{('Amazon', 'Kindle'): 2, ('Apple', 'iPad'): 2, ('Apple', 'iPhone'): 1}

此外,不要使用已弃用的
has_key()
。相反,请在关键字中使用
。例如,
如果fin\u dict:

中的值不能是列表,因为列表是可变的。您需要一个元组或其他不可变对象

我建议你改为使用计数器

>>> from collections import Counter
>>> dic = {'1d812hjbsa' : ['Apple', 'iPad'], '190usdnb1' : ['Amazon', 'Kindle'], 'sjdb1892': ['Apple', 'iPad'], '23ub8wh12' : ['Apple', 'iPhone'], '12ndsi01' : ['Amazon', 'Kindle']}
>>> counter = Counter(tuple(v) for v in dic.values())
>>> dict(counter)
{('Amazon', 'Kindle'): 2, ('Apple', 'iPad'): 2, ('Apple', 'iPhone'): 1}

此外,不要使用已弃用的
has_key()
。相反,请在
关键字中使用
。例如,如果fin dict:

中的值出现错误,因为列表不可损坏。例如,可以将列表转换为逗号分隔的字符串:

fin_dict = {}

for t in comb_dict:
    hash = ",".join(comb_dict[t])
    if not fin_dict.has_key(hash):
        fin_dict[hash] = 1
    else:
       fin_dict[hash] += 1

发生错误,因为列表不可损坏。例如,可以将列表转换为逗号分隔的字符串:

fin_dict = {}

for t in comb_dict:
    hash = ",".join(comb_dict[t])
    if not fin_dict.has_key(hash):
        fin_dict[hash] = 1
    else:
       fin_dict[hash] += 1
我看到两个问题:

  • 您正在使用方括号[]来定义dict,而不是大括号{}。方括号包围列表,而不是命令

  • 您试图在第二个dict(fin_dict)中使用列表作为键,这是不允许的。尝试引用comb_dict值的第二个元素作为fin_dict中的唯一键:

    comb_dict = {'1d812hjbsa' : ['Apple', 'iPad'], '190usdnb1' : ['Amazon', 'Kindle'], 'sjdb1892':['Apple', 'iPad'], '23ub8wh12' : ['Apple', 'iPhone'], '12ndsi01' : ['Amazon', 'Kindle'] }
    
    fin_dict={'iPad': 0, 'Kindle' : 0, 'iPhone' : 0}
    
    for key, value in comb_dict.items():
        if value[1] in fin_dict:
            fin_dict[value[1]] +=1
        else:
            print("unsupported device") # or exception handling
    
  • 我看到两个问题:

  • 您正在使用方括号[]来定义dict,而不是大括号{}。方括号包围列表,而不是命令

  • 您试图在第二个dict(fin_dict)中使用列表作为键,这是不允许的。尝试引用comb_dict值的第二个元素作为fin_dict中的唯一键:

    comb_dict = {'1d812hjbsa' : ['Apple', 'iPad'], '190usdnb1' : ['Amazon', 'Kindle'], 'sjdb1892':['Apple', 'iPad'], '23ub8wh12' : ['Apple', 'iPhone'], '12ndsi01' : ['Amazon', 'Kindle'] }
    
    fin_dict={'iPad': 0, 'Kindle' : 0, 'iPhone' : 0}
    
    for key, value in comb_dict.items():
        if value[1] in fin_dict:
            fin_dict[value[1]] +=1
        else:
            print("unsupported device") # or exception handling
    

  • 你的字典不是字典。你的字典不是字典。从我的角度看很蹩脚…完全忘记计数器…谢谢…:-)从我的角度看很蹩脚…完全忘记计数器…谢谢…:-)