gpt4 book ai didi

python - 如何遍历列表理解中的子元素?

转载 作者:太空宇宙 更新时间:2023-11-04 10:50:13 25 4
gpt4 key购买 nike

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

最佳答案

使用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

关于python - 如何遍历列表理解中的子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14468865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com