Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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组多个相似if语句_Python - Fatal编程技术网

python组多个相似if语句

python组多个相似if语句,python,Python,如何将大量类似的if-else语句或类似于以下良好实践的写作进行分组 if len(G72List) > 1 and G72List[1] != "": G7201Value = G72List[1] else: G7201Value = "" if len(G72List) > 5 and G72List[5] != "": G7205Value = G72List[5] else: G7

如何将大量类似的if-else语句或类似于以下良好实践的写作进行分组

if len(G72List) > 1 and G72List[1] != "":
    G7201Value = G72List[1]
else:
    G7201Value = ""

if len(G72List) > 5 and G72List[5] != "":
    G7205Value = G72List[5]
else:
    G7205Value = ""

if len(G72List) > 6 and G72List[6] != "":
    G7206Value = G72List[6]
else:
    G7206Value = ""

if len(G72List) > 7 and G72List[7] != "":
    G7207Value = G72List[7]
else:
    G7207Value = ""

if len(G72List) > 8 and G72List[8] != "":
    G7208Value = G72List[8]
else:
    G7208Value = ""

if len(G72List) > 9 and G72List[9] != "":
    G7209Value = G72List[9]
else:
    G7209Value = ""

if len(G72List) > 10 and G72List[10] != "":
    G7210Value = G72List[10]
else:
    G7210Value = ""

一系列只因数字不同的变量名可以重构为dict(如果从0开始,递增1,则可能是一个列表)。然后重复可以分解成for循环

GValue = {}
for i in [1, *range(5, 11)]:
    if len(G72List) > i and G72List[i] != "":
        GValue[7200+i] = G72List[i]
    else:
        GValue[7200+i] = ""

一系列只因数字不同的变量名可以重构为dict(如果从0开始,递增1,则可能是一个列表)。然后重复可以分解成for循环

GValue = {}
for i in [1, *range(5, 11)]:
    if len(G72List) > i and G72List[i] != "":
        GValue[7200+i] = G72List[i]
    else:
        GValue[7200+i] = ""

我找到了这种方法来演示您可以做什么:

from contextlib import suppress


G7201Value = G7205Value = G7206Value = G7207Value = G7209Value = G7210Value = 'empty'


for i in range(12):
    G72List = [_ for _ in range(i)]
    print(G72List)
    
    with suppress(IndexError):
        G7201Value = G72List[1]
        G7205Value = G72List[5]
        G7206Value = G72List[6]
        G7207Value = G72List[7]
        G7208Value = G72List[8]
        G7209Value = G72List[9]
        G7210Value = G72List[10]


    print(
        G7201Value,
        G7205Value,
        G7206Value,
        G7207Value,
        G7209Value,
        G7210Value
     )
输出:

$ python3 g27.py 
[]
empty empty empty empty empty empty
[0]
empty empty empty empty empty empty
[0, 1]
1 empty empty empty empty empty
[0, 1, 2]
1 empty empty empty empty empty
[0, 1, 2, 3]
1 empty empty empty empty empty
[0, 1, 2, 3, 4]
1 empty empty empty empty empty
[0, 1, 2, 3, 4, 5]
1 5 empty empty empty empty
[0, 1, 2, 3, 4, 5, 6]
1 5 6 empty empty empty
[0, 1, 2, 3, 4, 5, 6, 7]
1 5 6 7 empty empty
[0, 1, 2, 3, 4, 5, 6, 7, 8]
1 5 6 7 empty empty
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1 5 6 7 9 empty
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1 5 6 7 9 10
简单但有效;-)

让我解释一下:

您可以尝试查看列表是否足够长,然后检查要从中提取的索引的值是否有值,然后分配它,或者将值设置为
“”
。不需要这样做,只需使用空字符串一次初始化所有变量即可。 之后,使用
contextlib.suppress(indexer)
,允许在
suppress
之后缩进的代码段继续进行,只要未引发
indexer
;-)

免责声明:

for i in range(12):
    G72List = [_ for _ in range(i)]
    print(G72List)

只是为了演示的目的,用不同长度的列表向您展示行为。

我找到了一种演示您可以做什么的方法:

from contextlib import suppress


G7201Value = G7205Value = G7206Value = G7207Value = G7209Value = G7210Value = 'empty'


for i in range(12):
    G72List = [_ for _ in range(i)]
    print(G72List)
    
    with suppress(IndexError):
        G7201Value = G72List[1]
        G7205Value = G72List[5]
        G7206Value = G72List[6]
        G7207Value = G72List[7]
        G7208Value = G72List[8]
        G7209Value = G72List[9]
        G7210Value = G72List[10]


    print(
        G7201Value,
        G7205Value,
        G7206Value,
        G7207Value,
        G7209Value,
        G7210Value
     )
输出:

$ python3 g27.py 
[]
empty empty empty empty empty empty
[0]
empty empty empty empty empty empty
[0, 1]
1 empty empty empty empty empty
[0, 1, 2]
1 empty empty empty empty empty
[0, 1, 2, 3]
1 empty empty empty empty empty
[0, 1, 2, 3, 4]
1 empty empty empty empty empty
[0, 1, 2, 3, 4, 5]
1 5 empty empty empty empty
[0, 1, 2, 3, 4, 5, 6]
1 5 6 empty empty empty
[0, 1, 2, 3, 4, 5, 6, 7]
1 5 6 7 empty empty
[0, 1, 2, 3, 4, 5, 6, 7, 8]
1 5 6 7 empty empty
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1 5 6 7 9 empty
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1 5 6 7 9 10
简单但有效;-)

让我解释一下:

您可以尝试查看列表是否足够长,然后检查要从中提取的索引的值是否有值,然后分配它,或者将值设置为
“”
。不需要这样做,只需使用空字符串一次初始化所有变量即可。 之后,使用
contextlib.suppress(indexer)
,允许在
suppress
之后缩进的代码段继续进行,只要未引发
indexer
;-)

免责声明:

for i in range(12):
    G72List = [_ for _ in range(i)]
    print(G72List)

只是为了演示的目的,用不同长度的列表向您展示行为。

您应该能够将重复的代码重构为for循环。经验法则:如果变量的名称中开始出现数字,你应该考虑使用一个列表。你应该能够将重复的代码重构成for循环。一个经验法则:如果你的变量开始在他们的名字中得到数字,你应该考虑使用一个列表。我忘记添加一个1,然后跳过到5(见编辑)。不管怎样,如果你想在没有额外if else?@bakalolo的情况下加入循环,对于小范围,你只需将范围解压缩到一个包含额外项目的列表中即可。如果范围足够大,会占用太多内存,那么可以使用itertools.chain将iterables连接到单个迭代器中。我忘了添加1,然后它会跳到5(请参见编辑)。不管怎样,如果你想在没有额外if else?@bakalolo的情况下加入循环,对于小范围,你只需将范围解压缩到一个包含额外项目的列表中即可。如果范围足够大,会占用太多内存,则可以使用itertools.chain将iterables连接到单个迭代器中。