Python 如何遍历列表中的子元素?

Python 如何遍历列表中的子元素?,python,list,Python,List,我现在有一些代码,可以让我通过一个任意深度的键(比如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

我现在有一些代码,可以让我通过一个任意深度的键(比如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接受一个函数、一个输入列表和一个可选的初始值,然后将该函数重新应用于下一个元素,并从初始值开始最后一次调用的返回值。因此,它运行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
使用:

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

使用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、断点等。

使用for循环遍历参数没有什么错

>>> 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、断点等。

Arg;-。虽然我没有羔羊肉做出来。。。reducedict.\uuu getitem.\uuu,'author.age'.split.',d-+1再次击败我。@mgilson:你知道,对于这样的答案,我更喜欢使用lambda。未绑定的方法非常聪明,但只需要稍加解释即可-诚然,我有点喜欢lambda——如果我想到它,我会使用它的……调整:dict.get而不是dict.。uu getitem_uuu?@DSM:不,我不会使用dict.get;如果缺少键,则会得到TypeError:“NoneType”对象没有属性“\uuuu getitem\uuuuuuuu”,而不是KeyError:“missingkey”。我认为后者更容易调试-Arg;-。虽然我没有羔羊肉做出来。。。reducedict.\uuu getitem.\uuu,'author.age'.split.',d-+1再次击败我。@mgilson:你知道,对于这样的答案,我更喜欢使用lambda。未绑定的方法非常聪明,但只需要稍加解释即可-诚然,我有点喜欢lambda——如果我想到它,我会使用它的……调整:dict.get而不是dict.。uu getitem_uuu?@DSM:不,我不会使用dict.get;如果缺少键,则会得到TypeError:“NoneType”对象没有属性“\uuuu getitem\uuuuuuuu”,而不是KeyError:“missingkey”。我认为后者更容易调试-