简体   繁体   English

双循环列表理解

[英]list comprehension with double loops

i am a newbie in programming.I have a problem with a list comprehension.i need divide a list in tuple with size 5,and my code work well,but if i have in input a list of lists,i don't know how insert a double loop in list comprehension.i hope that someone can help me.我是编程新手。我的列表理解有问题。我需要将一个列表划分为大小为 5 的元组,我的代码运行良好,但如果我输入了一个列表列表,我不知道如何在列表理解中插入一个双循环。我希望有人可以帮助我。 this is my code:这是我的代码:

big_list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
x = 5
bot = [tuple(big_list1[i: i + x])for i in range(0, len(big_list1), x)]

and this is output:这是 output:

 bot=[(1, 2, 3, 4, 5), (6, 7, 8, 9, 10), (11, 12, 13, 14, 15)]

But if i have a list of lists like this:但是,如果我有这样的列表列表:

 my_list=[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5]]

I would like to have this:我想要这个:

res=[[(1, 2, 3, 4, 5),(6, 7, 8, 9, 10),(11, 12, 13, 14, 15)], [(1, 2, 3, 4, 5),(6, 7, 8, 9, 10)], [(1, 2, 3, 4, 5)]]

i am confused because having "range" in the loop, i don't know how to do the nested loop.我很困惑,因为循环中有“范围”,我不知道如何进行嵌套循环。

define as a function:定义为 function:

def split_in_tuples(input_list, tuple_length):
    return [tuple(input_list[i: i + tuple_length]) for i in range(0, len(input_list), tuple_length)]

which you can then use like:然后你可以使用它:

big_list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
split_in_tuples(big_list1, 5)

gives

[(1, 2, 3, 4, 5), (6, 7, 8, 9, 10), (11, 12, 13, 14, 15)]

then for your list of lists you can:那么对于您的列表列表,您可以:

my_list=[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5]]
[split_in_tuples(sublist, 5) for sublist in my_list]

which gives:这使:

[[(1, 2, 3, 4, 5), (6, 7, 8, 9, 10), (11, 12, 13, 14, 15)], [(1, 2, 3, 4, 5), (6, 7, 8, 9, 10)], [(1, 2, 3, 4, 5)]]

If you really want to use a list comprehension the you can do it as follows:如果你真的想使用列表推导,你可以这样做:

[[tuple(elem[i: i + x]) for i in range(0, len(elem), x)] for elem in my_list]

Maybe the final code is no so 'cool' anyway your code is not so far from the solution, you must add the external loop (in my solution I've used the same example with same naming) Maybe this can be useful也许最终的代码不是那么“酷”,无论如何你的代码离解决方案不远,你必须添加外部循环(在我的解决方案中,我使用了相同命名的相同示例)也许很有用

my_list=[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5]]
x = 5
[[tuple(big_list1[i: i + x])for i in range(0, len(big_list1), x)] for big_list1 in my_list]

# [[(1, 2, 3, 4, 5), (6, 7, 8, 9, 10), (11, 12, 13, 14, 15)],
 [(1, 2, 3, 4, 5), (6, 7, 8, 9, 10)],
 [(1, 2, 3, 4, 5)]]

that is what you want.这就是你想要的。

hint暗示

More in general, nesting lc requires a little hint.更一般地说,嵌套 lc 需要一点提示。 suppose you need to nest 2 list comprehension, the first approach is this:假设您需要嵌套 2 个列表理解,第一种方法是:

c = ['ab']
[a for a in b for b in c]

but this doesn't work NameError: name 'b' is not defined because the resolution order of python,但这不起作用NameError: name 'b' is not defined因为 python 的解析顺序,

but reordering in the right way, from right to left但以正确的方式重新排序,从右到左

[a for b in c for a in b]

run as expected.按预期运行。

If you want to use list comprehension, you can do it like this (given my_list and x from the stated question):如果你想使用列表理解,你可以这样做(给定my_listx来自所述问题):

[[tuple(l[i: i + x]) for i in range(0, len(l), x)] for l in my_list]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM