我现在有代码尝试让我按任意深度的键(如 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 循环来自动包含任意元素深度?