Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 遍历嵌套For语句_Python_Nested - Fatal编程技术网

Python 遍历嵌套For语句

Python 遍历嵌套For语句,python,nested,Python,Nested,我应该看看这个模型的解决方案,以便下一个课堂作业。此程序返回32到126(含)之间的ASCII值。我理解它,直到“为声明”。有人能帮我走过吗?我知道这与创建四个专栏有关,但我认为在继续之前理解其中的每一点是有益的 非常感谢 START = 32 END = 126 def GiveAscii(start=START, end=END, width=4): """Returns an ascii chart as a string. Readable.""" entrie

我应该看看这个模型的解决方案,以便下一个课堂作业。此程序返回32到126(含)之间的ASCII值。我理解它,直到“为声明”。有人能帮我走过吗?我知道这与创建四个专栏有关,但我认为在继续之前理解其中的每一点是有益的

非常感谢

START = 32

END = 126


def GiveAscii(start=START, end=END, width=4):

    """Returns an ascii chart as a string. Readable."""

    entries = end - start +1
    entries_per_column = entries/width
    if entries % width:
        entries_per_column += 1
    ret = []
    for row in range(entries_per_column):
        for column in range(width):
            entry = entries_per_column * column + row + start
            if entry > end:
                break
            ret += ["%3d = %-6s" % (entry, chr(entry))]
        ret += ['\n']
    return ''.join(ret)

def main():
    print GiveAscii()

if __name__ == '__main__':
    main()

第一个变量将从零到每列条目的值的范围枚举为名为行

对于每一行,都有一个从零到
width
值的枚举,作为名为
column

所以这是创建一个二维矩阵-应该是很容易理解的

对于
行中的每个
,该空间中的值被分配给变量
条目
。如果
条目
未超过矩阵的最大值,则将其作为列表放入返回列表
ret
。在此之后,
ret
被赋予一个换行符,以便可以直观地创建新行(当打印出
ret
时)。所以这个程序制作了一个列表,
ret
,它包含了一个二维的值矩阵——一系列的列表是
s,每个列表都包含一些称为
s的单值列表,其中包含
条目


我希望这是清楚的

我决定对您的代码进行注释,看看它是否能帮助您

START = 32

END = 126

# A function to return a string which represents an asci grid
def GiveAscii(start=START, end=END, width=4):

    """Returns an ascii chart as a string. Readable."""
    # The number of entries is the end minus the start plus one
    entries = end - start +1
    # Doing an integer devide provides us with the floor of the entries per column
    entries_per_column = entries/width
    # If our division was not even
    if entries % width:
        # We need to add an extra entry per column
        entries_per_column += 1
    # Initialize our array
    ret = []
    # For every row
    for row in range(entries_per_column):
        # Go through every column
        for column in range(width):
            # Do this math to create this entry, multiply first!
            entry = entries_per_column * column + row + start
            # If the entry is larger than the value end
            if entry > end:
                # Leave this column
                break
            # Not really sure what this formatting is but it is a formatting statment
            # Char casts your number as a character
            ret += ["%3d = %-6s" % (entry, chr(entry))]
         # When we are done with our column print and endline charachter
         ret += ['\n']
    # Return your array
    return ''.join(ret)

def main():
    print GiveAscii()

if __name__ == '__main__':
    main()

end
不应用于变量名。这是一个关键词。就实际建议而言,为什么不尝试在第二次
for
循环之后添加此语句
print“R:”,row,“C:”,column
@2rs2ts
end
不是python关键字。@Yossi不是吗?哎呀,我什么都懂了,但数学题是“条目=每列条目*列+行+开始”。你能解释一下为什么这个等式在这里吗?谢谢真棒的解释。谢谢我唯一感到困惑的地方是“进入”的方程式。这是:“条目=每列的条目*列+行+开始”。我不知道为什么,但我很难理解这个等式的逻辑…@user2376566,这个赋值正在为矩阵单元生成迭代值。如果您使用像PyCharm这样的IDE(有一个免费试用版!),您可以在这个语句上设置一个断点,然后一步一步地遍历它,看看每次遍历循环时都会发生什么。这肯定会帮助你了解发生了什么。