在 Python 中使用列表理解映射嵌套列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1306670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mapping a nested list with List Comprehension in Python?
提问by kjfletch
I have the following code which I use to map a nested list in Python to produce a list with the same structure.
我有以下代码用于在 Python 中映射嵌套列表以生成具有相同结构的列表。
>>> nested_list = [['Hello', 'World'], ['Goodbye', 'World']]
>>> [map(str.upper, x) for x in nested_list]
[['HELLO', 'WORLD'], ['GOODBYE', 'WORLD']]
Can this be done with list comprehension alone (without using the map function)?
这可以单独使用列表理解来完成吗(不使用 map 函数)?
回答by Eli Courtwright
For nested lists you can use nested list comprehensions:
对于嵌套列表,您可以使用嵌套列表推导式:
nested_list = [[s.upper() for s in xs] for xs in nested_list]
Personally I find map
to be cleaner in this situation, even though I almost always prefer list comprehensions. So it's really your call, since either will work.
就我个人而言,我发现map
在这种情况下更干净,尽管我几乎总是更喜欢列表理解。所以这真的是你的决定,因为两者都可以。
回答by Stuart Berg
Remember the Zen of Python:
记住 Python 的禅意:
There is generally more than one -- and probably several-- obvious ways to do it.**
通常有不止一种——可能还有几种——明显的方法来做到这一点。**
** Note: Edited for accuracy.
** 注意:为准确起见进行了编辑。
Anyway, I prefer map.
反正我更喜欢地图。
from functools import partial
nested_list = map( partial(map, str.upper), nested_list )
回答by KayEss
Map is certainly a much cleaner way of doing what you want. You can nest the list comprehensions though, maybe that's what you're after?
Map 当然是一种更简洁的方式来做你想做的事。不过,您可以嵌套列表理解,也许这就是您所追求的?
[[ix.upper() for ix in x] for x in nested_list]
回答by Oldyoung
Here is solution for nested list that has arbitrary depth:
这是具有任意深度的嵌套列表的解决方案:
def map_nlist(nlist=nlist,fun=lambda x: x*2):
new_list=[]
for i in range(len(nlist)):
if isinstance(nlist[i],list):
new_list += [map_nlist(nlist[i],fun)]
else:
new_list += [fun(nlist[i])]
return new_list
you want to upper case all you list element, just type
你想大写所有你列出的元素,只需输入
In [26]: nested_list = [['Hello', 'World'], ['Goodbye', [['World']]]]
In [27]: map_nlist(nested_list,fun=str.upper)
Out[27]: [['HELLO', 'WORLD'], ['GOODBYE', [['WORLD']]]]
And more important, this recursive function can do more than this!
更重要的是,这个递归函数可以做的远不止这些!
I am new to python, feel free to discuss!
我是python新手,欢迎讨论!
回答by Karl Anderson
Other posters have given the answer, but whenever I'm having trouble wrapping my head around a functional construct, I swallow my pride and spell it out longhand with explicitly non-optimal methods and/or objects. You said you wanted to end up with a generator, so:
其他海报已经给出了答案,但是每当我在功能结构上遇到困难时,我都会吞下我的骄傲,并用明确的非最佳方法和/或对象直接拼写出来。你说你想要一个生成器,所以:
for xs in n_l:
def doUpper(l):
for x in l:
yield x.upper()
yield doUpper(xs)
for xs in n_l:
yield (x.upper() for x in xs)
((x.upper() for x in xs) for xs in n_l)
Sometimes it's cleaner to keep one of the longhand versions. For me, map and reduce sometimes make it more obvious, but Python idioms might be more obvious for others.
有时,保留一个普通版本会更干净。对我来说,map 和 reduce 有时会更明显,但 Python 习语对其他人可能更明显。