python中字典操作列表 - TypeError:unhashable类型:' dict'

时间:2016-06-02 05:10:12

标签: python

我有一个下面的表格列表:

<script type="text/javascript">
   $("#submit").on("click",function() {
   if ($("#title").val() != '') {
        $("#loading").css("display", "none"); 
    }
    else {
        $("#loading").css("display", "block");
        setTimeout(function(){ $('#loading').fadeOut() }, 5000); 
    } 

});
</script>

转换为

oldlist = [{'x': {'a':1,'b':2}, 'y':2},{'x':{'a':6,'b':7}, 'y':2},{'x':{'a':1,'b':2}, 'y':3},{'x':{'a':1,'b':2}, 'y':2},{'x':{'a':10,'b':11}, 'y':4}]  

我试过了

final = [{'x':{'a':1,'b':2},'y':[2,3,2],'count':3},{'x':{'a':6,'b':7},'y':[2],'count':1},{'x':{'a':10,'b':11},'y':[4],'count':1}]  

获取

  

s =设定([d [&#39; x&#39;]为旧名单中的d])
  TypeError:不可用类型:&#39; dict&#39;

有更简单的方法吗?另外我知道x只能有三个值,所以我创建了三个变量list1,list2和list3。如果x可以有其他几个值,我必须找到类似的词典列表,如final!它也适用于字符串!

编辑:我试过这个。但这一切都搞砸了

oldlist = [{'x': {'a':1,'b':2}, 'y':2},{'x':{'a':6,'b':7}, 'y':2},{'x':{'a':1,'b':2}, 'y':3},{'x':{'a':1,'b':2}, 'y':2},{'x':{'a':10,'b':11}, 'y':4}]  
list1=[]  
list2=[]  
list3=[]  
s = set([d['x'] for d in oldlist])  
news=list(s)  
for item in oldlist:  
if item['x'] == news[0]:  
      list1.append(item['y'])  

if item['x'] == news[1]:  
      list2.append(item['y'])  

if item['x'] == news[2]:  
      list3.append(item['y'])  
final=[]  
dic1 = {'x':news[0],'y':list1,'count':len(list1)}  
dic2 = {'x':news[1],'y':list2,'count':len(list2)}  
dic3 = {'x':news[2],'y':list3,'count':len(list3)}  
final.append(dic1)  
final.append(dic2)  
final.append(dic3)  
print final

3 个答案:

答案 0 :(得分:3)

set函数只能处理hashable个对象,如字符串,数字,元组e.t.c

List,dict等数据类型是不可用的类型,因此set函数无法处理它们。

更清晰一点:

What do you mean by hashable in Python?

http://blog.lerner.co.il/is-it-hashable-fun-and-games-with-hashing-in-python/

您需要的基本实现:

for elem in oldlist:
    found = False
    for item in newlist:
        if elem['x'] == item['x']:
            y = item.get('y',[])
            item['y'] = t.append(elem['y'])
            found = True
            break
    if not found:
        newlist.append({'x':elem['x'], 'y':[elem['y']]})

这将为您提供预期的结果

答案 1 :(得分:1)

设置python的功能不允许使用词典,你不能强制它,尝试另一种方法。 (仔细看看第5 第6 行的评论)

试试这段代码:

oldlist = [{'x': {'a':1,'b':2}, 'y':2},{'x':{'a':6,'b':7}, 'y':2},{'x':{'a':1,'b':2}, 'y':3},{'x':{'a':1,'b':2}, 'y':2},{'x':{'a':10,'b':11}, 'y':4}]  
list1=[]  
list2=[]  
list3=[]  
s = [d['x'] for d in oldlist] # Placed the dictionaries in a list
s = result = [dict(tupleized) for tupleized in set(tuple(item.items()) for item in s)] # This is the manual way on removing duplicates dictionaries in a list instead of using set
news=list(s)  
for item in oldlist:  
    if item['x'] == news[0]:  
        list1.append(item['y'])  

    if item['x'] == news[1]:  
        list2.append(item['y'])  

    if item['x'] == news[2]:  
        list3.append(item['y']) 

final=[]  
dic1 = {'x':news[0],'y':list1,'count':len(list1)}  
dic2 = {'x':news[1],'y':list2,'count':len(list2)}  
dic3 = {'x':news[2],'y':list3,'count':len(list3)}  
final.append(dic1)  
final.append(dic2)  
final.append(dic3)  
print final

答案 2 :(得分:1)

您可以使用defaultdict其中键是frozenset个对象,这些对象是从原始字母中的x值创建的,值是相对y的列表。然后,您可以使用列表推导构建最终结果,并将frozensets转回dicts:

from collections import defaultdict

oldlist = [{'x': {'a':1,'b':2}, 'y':2},{'x':{'a':6,'b':7}, 'y':2},{'x':{'a':1,'b':2}, 'y':3},{'x':{'a':1,'b':2}, 'y':2},{'x':{'a':10,'b':11}, 'y':4}]
res = defaultdict(list)
for d in oldlist:
    res[frozenset(d['x'].items())].append(d['y'])

final = [{'x': dict(k), 'y': v, 'count': len(v)} for k, v in res.items()] # [{'y': [2, 3, 2], 'x': {'a': 1, 'b': 2}, 'count': 3}, {'y': [4], 'x': {'a': 10, 'b': 11}, 'count': 1}, {'y': [2], 'x': {'a': 6, 'b': 7}, 'count': 1}]
相关问题