Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops_For Loop - Fatal编程技术网

Python 如何使for循环的结果在该循环之外可用?

Python 如何使for循环的结果在该循环之外可用?,python,loops,for-loop,Python,Loops,For Loop,我有这样的想法: for line in handle: line = line.rstrip() if not line.startswith('From') : continue words = line.split() if words[0] != 'From' : continue email=words[1] print email emailsList = [] for line in handle: line = line.

我有这样的想法:

for line in handle:
    line = line.rstrip()
    if not line.startswith('From') : continue
    words = line.split()
    if words[0] != 'From' : continue
    email=words[1]
    print email
emailsList = []
for line in handle:
    line = line.rstrip()
    if not line.startswith('From') : continue
    words = line.split()
    if words[0] != 'From' : continue
    email=words[1]
    print email
    emailsList.append (email)
print emailsList

我希望电子邮件结果也能在
for
循环之外获得。如何使用?

您很幸运,python作用域是最好的,您会发现电子邮件不在for循环的范围内,因为python作用域规则声明如下:

引述自:

立法局规则。 L.本地。(在函数(def或lambda)中以任何方式分配的名称),并且在该函数中未声明为全局的

E.封闭函数局部变量。(任何和所有封闭函数(def或lambda)的本地范围内的名称,从内到外

G.Global(模块)。在模块文件的顶层指定的名称,或在文件中的def中声明的全局名称

B.内置(Python)。在内置名称模块中预先分配的名称:open、range、SyntaxError

其中,
for
关键字不是any的成员。在
for
循环中声明的任何变量的作用域都将限定为其最外层的作用域。在这种情况下,封闭函数

def some_func():
    for line in handle:
        line = line.rstrip()
        if not line.startswith('From') : continue
        words = line.split()
        if words[0] != 'From' : continue
        email=words[1]

    print email

将打印电子邮件的值。

您很幸运,python作用域是最好的,您会发现电子邮件在for循环的范围之外可用,因为python作用域规则声明如下:

引述自:

立法局规则。 L.Local.(在函数(def或lambda)中以任何方式分配的名称),并且在该函数中未声明为全局名称

E.封闭函数局部变量。(任何和所有封闭函数(def或lambda)的局部范围中的名称,从内到外

G.Global(模块)。在模块文件的顶层指定的名称,或在文件中的def中声明的全局名称

B.内置(Python)。在内置名称模块中预先分配的名称:open、range、SyntaxError

其中,
for
关键字不是any的成员。在
for
循环中声明的任何变量的作用域都将限定为其最外层的作用域。在这种情况下,封闭函数

def some_func():
    for line in handle:
        line = line.rstrip()
        if not line.startswith('From') : continue
        words = line.split()
        if words[0] != 'From' : continue
        email=words[1]

    print email

将打印电子邮件的值。

您很幸运,python作用域是最好的,您会发现电子邮件在for循环的范围之外可用,因为python作用域规则声明如下:

引述自:

立法局规则。 L.Local.(在函数(def或lambda)中以任何方式分配的名称),并且在该函数中未声明为全局名称

E.封闭函数局部变量。(任何和所有封闭函数(def或lambda)的局部范围中的名称,从内到外

G.Global(模块)。在模块文件的顶层指定的名称,或在文件中的def中声明的全局名称

B.内置(Python)。在内置名称模块中预先分配的名称:open、range、SyntaxError

其中,
for
关键字不是any的成员。在
for
循环中声明的任何变量的作用域都将限定为其最外层的作用域。在这种情况下,封闭函数

def some_func():
    for line in handle:
        line = line.rstrip()
        if not line.startswith('From') : continue
        words = line.split()
        if words[0] != 'From' : continue
        email=words[1]

    print email

将打印电子邮件的值。

您很幸运,python作用域是最好的,您会发现电子邮件在for循环的范围之外可用,因为python作用域规则声明如下:

引述自:

立法局规则。 L.Local.(在函数(def或lambda)中以任何方式分配的名称),并且在该函数中未声明为全局名称

E.封闭函数局部变量。(任何和所有封闭函数(def或lambda)的局部范围中的名称,从内到外

G.Global(模块)。在模块文件的顶层指定的名称,或在文件中的def中声明的全局名称

B.内置(Python)。在内置名称模块中预先分配的名称:open、range、SyntaxError

其中,
for
关键字不是any的成员。在
for
循环中声明的任何变量的作用域都将限定为其最外层的作用域。在这种情况下,封闭函数

def some_func():
    for line in handle:
        line = line.rstrip()
        if not line.startswith('From') : continue
        words = line.split()
        if words[0] != 'From' : continue
        email=words[1]

    print email

将打印电子邮件的值。

如果要存储所有电子邮件并访问它们之外的循环,请考虑使用这样的列表:

for line in handle:
    line = line.rstrip()
    if not line.startswith('From') : continue
    words = line.split()
    if words[0] != 'From' : continue
    email=words[1]
    print email
emailsList = []
for line in handle:
    line = line.rstrip()
    if not line.startswith('From') : continue
    words = line.split()
    if words[0] != 'From' : continue
    email=words[1]
    print email
    emailsList.append (email)
print emailsList
并通过索引访问列表中的每个元素,如:

print emailsList [0]

如果您想存储所有电子邮件并访问它们之外的循环,请考虑使用这样的列表:

for line in handle:
    line = line.rstrip()
    if not line.startswith('From') : continue
    words = line.split()
    if words[0] != 'From' : continue
    email=words[1]
    print email
emailsList = []
for line in handle:
    line = line.rstrip()
    if not line.startswith('From') : continue
    words = line.split()
    if words[0] != 'From' : continue
    email=words[1]
    print email
    emailsList.append (email)
print emailsList
并通过索引访问列表中的每个元素,如:

print emailsList [0]

如果您想存储所有电子邮件并访问它们之外的循环,请考虑使用这样的列表:

for line in handle:
    line = line.rstrip()
    if not line.startswith('From') : continue
    words = line.split()
    if words[0] != 'From' : continue
    email=words[1]
    print email
emailsList = []
for line in handle:
    line = line.rstrip()
    if not line.startswith('From') : continue
    words = line.split()
    if words[0] != 'From' : continue
    email=words[1]
    print email
    emailsList.append (email)
print emailsList
并通过索引访问列表中的每个元素,如:

print emailsList [0]

如果您想存储所有电子邮件并访问它们之外的循环,请考虑使用这样的列表:

for line in handle:
    line = line.rstrip()
    if not line.startswith('From') : continue
    words = line.split()
    if words[0] != 'From' : continue
    email=words[1]
    print email
emailsList = []
for line in handle:
    line = line.rstrip()
    if not line.startswith('From') : continue
    words = line.split()
    if words[0] != 'From' : continue
    email=words[1]
    print email
    emailsList.append (email)
print emailsList
并通过索引访问列表中的每个元素,如:

print emailsList [0]

Mike McMahon的回答是正确的,分配给
email
变量的值将在循环外可用。但是,他显示的函数非常脆弱。如果您的文件中没有任何以
开头的行“From”
,变量将永远不会被赋值,当您尝试打印它时,将出现异常。此外,如果多行符合条件(例如,从“”开始,以“
”开头),您将只看到最后一行。如果您想要第一行,则需要更改内容

这里有一个更好的结构:

emails = []
for line in handle:
    line = line.rstrip()
    if line.startswith('From'):
        words = line.split()
        if words[0] == 'From':
            emails.append(words[1])
我发现嵌套的
if
块比重复的
continue
语句更容易理解,尽管这是一种风格选择。此版本列出了它找到的所有值。列表可能为空(如果没有以
From
开头的行,或者可能有一个或多个结果)

如果您只想要第一个结果,而没有找到任何结果是错误的,那么下面是一个