按列表列表对字典进行排序

时间:2014-01-30 05:55:55

标签: python sorting dictionary

我正在尝试按列表列表对字典进行排序。列表列表中的项目是字典中的键。之前我问过,但答案没有解决问题。

我的输入列表是:

   mylist= [
    ['why', 'was', 'cinderella', 'late', 'for', 'the', 'ball', 'she', 'forgot', 'to', 'swing', 'the', 'bat'],
    ['why', 'is', 'the', 'little', 'duck', 'always', 'so', 'sad', 'because', 'he', 'always', 'sees', 'a', 'bill', 'in', 'front', 'of', 'his', 'face'],
    ['what', 'has', 'four', 'legs', 'and', 'goes', 'booo', 'a', 'cow', 'with', 'a', 'cold'], 
    ['what', 'is', 'a', 'caterpillar', 'afraid', 'of', 'a', 'dogerpillar'],
    ['what', 'did', 'the', 'crop', 'say', 'to', 'the', 'farmer', 'why', 'are', 'you', 'always', 'picking', 'on', 'me']
    ]

我的词典有点像这样:

    myDict = {'to': [7, 11, 17, 23, 24, 25, 26, 33, 34, 37, 39, 41, 47, 48, 53, 56], 
    'jam': [20], 'black': [5], 'farmer': [11],
    'woodchuck': [54], 'has': [14, 16, 51], 'who': [16]
    }

我的代码是:

    def sort_by_increasing_order(mylist, myDict):
    #temp = sorted(myDict, key=myDict.get)
    temp = sorted(myDict, key=lambda tempKey: for tempKey in mylist, reverse=True )
    return temp

类似的东西:

    sort_by_increasing_order(['d', 'e', 'f'], {'d': [0, 1], 'e': [1, 2, 3], 'f': [4]})
    result: ['f', 'e', 'd']

因此,对于我的示例输入,它看起来像:

        sort_by_increasing_order(mylist, myDict)
        >> ['to','woodchuck','has','jam','who','farmer']

当我尝试按列表排序时,注释行只是按字典键排序。我的方法不正确。结果应该是一个列表,如上所述,索引长度的顺序递增。任何建议。

3 个答案:

答案 0 :(得分:2)

参考@doukremt回答 假设你知道装饰者。

mydict = {'d': [0, 1], 'e': [1, 2, 3], 'f': [4]}
mylist = [['d', 'e', 'f', 'c'], ['c', 'v', 'd', 'n']]

def convert_to_set(mylist, result_set=None):
    if result_set is None:
        result_set = []
    for item in mylist:
        if isinstance(item, str):
            result_set.append(item)
        if isinstance(item, list):
            convert_to_set(item, result_set)
    return set(result_set)

def list_to_set(f):
    def wrapper(mylist, mydict):
        myset = convert_to_set(mylist)
        result = f(myset, mydict)
        return result
    return wrapper

@list_to_set
def findit(mylist, mydict):
    gen = ((k, mydict[k]) for k in mylist if k in mydict)
    return [k for k, v in sorted(gen, key=lambda p: len(p[1]))]

print findit(mylist, mydict)

答案 1 :(得分:1)

>>> D= {'to': [7, 11, 17, 23, 24, 25, 26, 33, 34, 37, 39, 41, 47, 48, 53, 56], 
...     'jam': [20], 'black': [5], 'farmer': [11],
...     'woodchuck': [54], 'has': [14, 16, 51], 'who': [16]
...     }
>>> 
>>> sorted(D, key=lambda k:len(D[k]), reverse=True)
['to', 'has', 'who', 'jam', 'black', 'farmer', 'woodchuck']

对于值

>>> sorted(D.values(), key=len, reverse=True)
[[7, 11, 17, 23, 24, 25, 26, 33, 34, 37, 39, 41, 47, 48, 53, 56], [14, 16, 51], [16], [20], [5], [11], [54]]

对于(键,值)

>>> sorted(D.items(), key=lambda i:len(i[1]), reverse=True)
[('to', [7, 11, 17, 23, 24, 25, 26, 33, 34, 37, 39, 41, 47, 48, 53, 56]), ('has', [14, 16, 51]), ('who', [16]), ('jam', [20]), ('black', [5]), ('farmer', [11]), ('woodchuck', [54])]

编辑:仍然不太清楚你要求的是什么。你的例子似乎并不关心长度,否则“有”应该出现在“土拨鼠”之前?将len更改为max可能就是您想要的

>>> D = {'to': [7, 11, 17, 23, 24, 25, 26, 33, 34, 37, 39, 41, 47, 48, 53, 56], 
...     'jam': [20], 'black': [5], 'farmer': [11],
...     'woodchuck': [54], 'has': [14, 16, 51], 'who': [16]
...     }
>>> 
>>> sorted(D, key=lambda k:max(D[k]), reverse=True)
['to', 'woodchuck', 'has', 'jam', 'who', 'farmer', 'black']
>>> sorted(D.values(), key=max, reverse=True)
[[7, 11, 17, 23, 24, 25, 26, 33, 34, 37, 39, 41, 47, 48, 53, 56], [54], [14, 16, 51], [20], [16], [11], [5]]
>>> sorted(D.items(), key=lambda i:max(i[1]), reverse=True)
[('to', [7, 11, 17, 23, 24, 25, 26, 33, 34, 37, 39, 41, 47, 48, 53, 56]), ('woodchuck', [54]), ('has', [14, 16, 51]), ('jam', [20]), ('who', [16]), ('farmer', [11]), ('black', [5])]

答案 2 :(得分:1)

def findit(mylist, mydict):
    gen = ((k, mydict[k]) for k in mylist if k in mydict)
    return [k for k, v in sorted(gen, key=lambda p: len(p[1]))]


>>> findit(['d', 'e', 'f'], {'d': [0, 1], 'e': [1, 2, 3], 'f': [4]})
['f', 'd', 'e']