1

我现在有代码尝试让我按任意深度的键(如 mongo)对字典进行排序,但它要求我硬编码键的深度。

#This is the code for inside the function, you need to make a function to receive the  arguments
#and the dictionary. the arguments should be listed in order of layers. It then splits the argument string
#at the "." and assigns to each of the items.From there it should return the "l[]".
#you need to set it up to pass the arguments to these appropriate spots. so the list of dicts goes to
#list and the arguments go to argstring and it should be taken care of from there.


#splitting the argument
argstring="author.age"
arglist = argstring.split(".")

x=(5-len(arglist))#need to set this number to be the most you want to accept
while x>0:
    arglist.append('')
    x-=1

#test list
list = [
{'author' : {'name':'JKRowling','age':47,'bestseller':{'series':'harrypotter','copiessold':12345}}},


{'author' : {'name':'Tolkien','age':81,'bestseller':{'series':'LOTR','copiessold':5678}}},


{'author' : {'name':'GeorgeMartin','age':64,'bestseller':{'series':'Fire&Ice','copiessold':12}}},


{'author' : {'name':'UrsulaLeGuin','age':83,'bestseller':{'series':'EarthSea', 'copiessold':444444}}}
]
l=[]#the list for returning


#determining sort algorythm
l = sorted(list, key=lambda e: e[arglist[0]][arglist[1]])#need add as many of these as necesarry to match the number above
print()

这可行,但必须手动指定 arglist 中的参数似乎很愚蠢。如果我需要 5 深,我需要手动指定 e 5 次。有没有办法使用列表理解或 for 循环来自动包含任意元素深度?

4

2 回答 2

4

使用reduce()

sorted(list, key=lambda e: reduce(lambda m, k: m[k], argslist, e))

reduce()接受一个函数、一个输入列表和一个可选的初始值,然后将该函数重新应用于下一个元素和最后一次调用的返回值(从初始值开始)。因此,它在从 中获取m[k0][k1][k2]..[kn]连续k值的地方运行argslist

简短演示:

>>> e = {'author' : {'name':'JKRowling','age':47,'bestseller':{'series':'harrypotter','copiessold':12345}}}
>>> argslist = ['author', 'age']
>>> reduce(lambda m, k: m[k], argslist, e)
47
于 2013-01-22T22:05:10.043 回答
0

使用for循环遍历args没有错

>>> e = {'author' : {'name':'JKRowling','age':47,'bestseller':{'series':'harrypotter','copiessold':12345}}}
>>> argslist = ['author', 'age']
>>> result = e
>>> for arg in argslist:
...     result = result[arg]
... 
>>> result
47

这种方式真的很容易调试,可以放try/except, print, 断点等。

于 2013-01-22T22:25:57.413 回答