列表理解问题:一对多映射? [英] List Comprehension Question: One to Many Mapping?
问题描述
大家好,
如何将列表映射到列表推导的两个列表?
例如,如果我有x = [[1,2],[3,4]]
我想要的是一个包含四个子列表的新列表:
[[1,2],[f(1),f(2)],[3,4],[f(3),f(4)]]
[1,2]映射到[1,2]和[f(1),f(2)]和[3,4]映射到
[3,4] ,[f(3),f(4)]。
我无法通过list comprension找到任何方法。我结束了
使用循环(基于实际代码的未经测试的代码):
l = []
$ x $ b for x in x :
l.append(y)
l.append([f(z)for z in y])
谢谢,
Geoffrey
8月23日晚上9:24,初学者< zyzhu2。 .. @ gmail.comwrote:
大家好,
如何将列表映射到列表理解的两个列表?
例如,如果我有x = [[1,2],[3,4]]
我想要的是列表的新列表有四个子列表:
[[1,2],[f(1),f(2)],[3,4],[ f(3),f(4)]]
[1,2]映射到[1,2]和[f(1),f(2)]和[ [3,4],[f(3),f(4)]。
找到与list comprension相关的任何方法。我结束了
使用循环(基于实际代码的未经测试的代码):
l = []
$ x $ b for x in x :
l.append(y)
l.append([f(z)for z in y])
假设f是:
>> def f(n): return str(n)
> ;> import itertools
使用列表解析:
>> [i for i in itertools.chain(* [(eachx,[f(y)for y in eachx])for eachx in x] )]
[[1,2],['''',''2''],[3, 4],[''3'',''4'']
Usin ga list:
>> list(itertools.chain(* [(eachx, [f(y)for each in eachx])for eachx in x]))
[[1,2],[ ''1',''''',[3,4],[''3'',''4'']
两种情况都不太好。
-
希望这会有所帮助,
史蒂文
< blockquote> beginner写道:
如何将列表映射到列表推导的两个列表?
例如,如果我有x = [[1,2],[3,4]]
我想要的是一个包含四个子列表的新列表:
[[1,2],[f(1),f(2)],[3,4],[f(3),f(4)]]
[1,2]映射到[1,2]和[f(1),f(2)]和[3,4]映射到
[3] ,4],[f(3),f(4)]。
我无法通过list compr找到任何方法ension。
>> [a for b in((item,map (f,item))x)中的项目为b]
[[1,2],[f( 1),f(2)],[3,4],[f(3),f(4)]]
我结束了
使用循环(基于实际代码的未经测试的代码):
l = []
$ x $ b for x in x:
l.append(y)
l.append([f(z)for z in y])
使用循环在这种情况下给你更清晰的代码,所以我会用它。
Peter
8月23日,9: 24 pm,初学者< zyzhu2 ... @ gmail.comwrote:
大家好,
如何映射一个列表到列表理解的两个列表?
例如,如果我有x = [[1,2],[3,4]]
我想要的是一个新的列表列表有四个子列表:
[[1,2],[f(1),f(2)],[3,4],[f(3),f (4)]]
[1,2]映射到[1,2]和[f(1),f(2)]和[3,4]映射
[3,4],[f(3),f(4)]。
我找不到任何办法与列表comprension。我结束了
使用循环(基于实际代码的未经测试的代码):
l = []
$ x $ b for x in x :
l.append(y)
l.append([f(z)for z in y])
谢谢,
Geoffrey
这可能是你想要的:
l = [[y ,[y中的z的[f(z)]] x中的y]
但它有点密集。怎么样的 :
l = []
$ x $ b for x in x:
Fy = [f(z)for z in y]
l.extend([y,Fy])
- David
Hi All,
How do I map a list to two lists with list comprehension?
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
[3,4], [f(3), f(4)].
I just can''t find any way to do that with list comprension. I ended up
using a loop (untested code based on real code):
l=[]
for y in x:
l.append(y)
l.append([f(z) for z in y])
Thanks,
Geoffrey
On Aug 23, 9:24 pm, beginner <zyzhu2...@gmail.comwrote:Hi All,
How do I map a list to two lists with list comprehension?
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
[3,4], [f(3), f(4)].
I just can''t find any way to do that with list comprension. I ended up
using a loop (untested code based on real code):
l=[]
for y in x:
l.append(y)
l.append([f(z) for z in y])
Suppose f is:
>>def f(n): return str(n)
>>import itertools
Using a list comprehension:
>>[i for i in itertools.chain(*[(eachx, [f(y) for y in eachx]) for eachx in x])]
[[1, 2], [''1'', ''2''], [3, 4], [''3'', ''4'']]
Using a list:
>>list(itertools.chain(*[(eachx, [f(y) for y in eachx]) for eachx in x]))
[[1, 2], [''1'', ''2''], [3, 4], [''3'', ''4'']]
Not so pretty in either case.
--
Hope this helps,
Steven
beginner wrote:
How do I map a list to two lists with list comprehension?
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
[3,4], [f(3), f(4)].
I just can''t find any way to do that with list comprension.
>>[a for b in ((item, map(f, item)) for item in x) for a in b]
[[1, 2], [f(1), f(2)], [3, 4], [f(3), f(4)]]
I ended up
using a loop (untested code based on real code):
l=[]
for y in x:
l.append(y)
l.append([f(z) for z in y])Using a loop gives you clearer code in this case, so I''d use that.
Peter
On Aug 23, 9:24 pm, beginner <zyzhu2...@gmail.comwrote:Hi All,
How do I map a list to two lists with list comprehension?
For example, if I have x=[ [1,2], [3,4] ]
What I want is a new list of list that has four sub-lists:
[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
[1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
[3,4], [f(3), f(4)].
I just can''t find any way to do that with list comprension. I ended up
using a loop (untested code based on real code):
l=[]
for y in x:
l.append(y)
l.append([f(z) for z in y])
Thanks,
GeoffreyThis may be what you want:
l = [[y, [f(z) for z in y]] for y in x]
But It''s a bit dense. How about:
l=[]
for y in x:
Fy = [f(z) for z in y]
l.extend([y, Fy])
-- David
这篇关于列表理解问题:一对多映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!