单行嵌套循环 [英] Single Line Nested For Loops

查看:124
本文介绍了单行嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  def转置(m):
height = len (m)
width = len(m [0])
return [[m [i] [j]为范围(0,高度)]为范围(0,宽度)]

在这个过程中,我意识到我不完全理解单行嵌套for循环是如何执行的。请回答以下问题来帮助我理解:
$ b $ ol

  • 这个for循环执行的顺序是什么?

  • 如果我有一个三重嵌套for循环,它会执行什么命令?

  • 什么会等于相等的unnesnes for循环?

  • <



      [function(i,j)for i,j在对象中] 




    1. 必须使用什么类型的对象才能使用它循环结构?
    2. i和j分配给对象中的元素的顺序是什么?

    3. 是否可以用不同的for循环结构?

    4. 这个for循环可以嵌套一个类似或不同的循环结构吗?而且它看起来如何呢?

    其他信息同样值得赞赏

    解决方案

    最好的信息来源是关于列表解析的官方Python教程。列表推导与循环几乎相同(当然,任何列表理解都可以写成一个for循环),但是它们通常比使用for循环快。



    在本教程的更长的列表理解( if if 部分过滤理解,只有通过if语句的部分被传递到列表理解的最后部分(这里<$
    $ b $ pre $ code>>>>> [(($,$) (1,3),(1,4),(2,x,y)对于x in [1,2,3] 3),(2,1),(2,4),(3,1),(3,4)]

    它和嵌套的for循环完全一样(并且,正如本教程所述,请注意for和if的顺序是如何相同的)。

    <$对于x in [1,2,3],
    ...对于y $ p $>>> combs = []
    >>> in [3,1,4]:
    ... if x!= y:
    ... combs.append((x,y))
    ...
    (1,3),(1,4),(2,3),(2,1),(2,4),(3, 1),(3,4)]

    主要的区别列表理解和for循环是for循环的最后一部分(在哪里做的事情)是在开始的时候而不是在结尾。

    在你的问题上:

    lockquote

    为了使用循环结构,必须使用什么类型的对象?


    可迭代。任何可以生成(有限)一组元素的对象。这些包括任何容器,列表,集合,生成器等。


    i和j分配给对象元素的顺序是什么?

    它们的分配顺序与每个列表生成的顺序完全相同,就好像它们在嵌套for循环中一样对于你的第一个理解,你会得到1元素为我,然后从j的每个值,第二个元素到我,然后从j的每个值,等等。)


    可以用不同的循环结构模拟吗?

    是的,已经在上面显示过了。


    这个for循环可以嵌套一个相似或不同的循环结构吗?它看起来如何?


    当然,但这不是一个好主意。在这里,例如,给你一个字符列表:

      [[ch ch in word] for word in apple,banana,pear,the,hello)] 


    Wrote this function in python that transposes a matrix:

    def transpose(m):
        height = len(m)
        width = len(m[0])
        return [ [ m[i][j] for i in range(0, height) ] for j in range(0, width) ]
    

    In the process I realized I don't fully understand how single line nested for loops execute. Please help me understand by answering the following questions:

    1. What is the order in which this for loop executes?
    2. If I had a triple nested for loop, what order would it execute?
    3. What would be equal the equal unnested for loop?

    Given,

    [ function(i,j) for i,j in object ]
    

    1. What type must object be in order to use this for loop structure?
    2. What is the order in which i and j are assigned to elements in object?
    3. Can it be simulated by a different for loop structure?
    4. Can this for loop be nested with a similar or different structure for loop? And how would it look?

    Additional information is appreciated as well.

    解决方案

    The best source of information is the official Python tutorial on list comprehensions. List comprehensions are nearly the same as for loops (certainly any list comprehension can be written as a for-loop) but they are often faster than using a for loop.

    Look at this longer list comprehension from the tutorial (the if part filters the comprehension, only parts that pass the if statement are passed into the final part of the list comprehension (here (x,y)):

    >>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
    [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
    

    It's exactly the same as this nested for loop (and, as the tutorial says, note how the order of for and if are the same).

    >>> combs = []
    >>> for x in [1,2,3]:
    ...     for y in [3,1,4]:
    ...         if x != y:
    ...             combs.append((x, y))
    ...
    >>> combs
    [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
    

    The major difference between a list comprehension and a for loop is that the final part of the for loop (where you do something) comes at the beginning rather than at the end.

    On to your questions:

    What type must object be in order to use this for loop structure?

    An iterable. Any object that can generate a (finite) set of elements. These include any container, lists, sets, generators, etc.

    What is the order in which i and j are assigned to elements in object?

    They are assigned in exactly the same order as they are generated from each list, as if they were in a nested for loop (for your first comprehension you'd get 1 element for i, then every value from j, 2nd element into i, then every value from j, etc.)

    Can it be simulated by a different for loop structure?

    Yes, already shown above.

    Can this for loop be nested with a similar or different structure for loop? And how would it look?

    Sure, but it's not a great idea. Here, for example, gives you a list of lists of characters:

    [[ch for ch in word] for word in ("apple", "banana", "pear", "the", "hello")]
    

    这篇关于单行嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆