gpt4 book ai didi

python - 在多个 For 循环中追加到类列表

转载 作者:行者123 更新时间:2023-11-30 21:54:20 25 4
gpt4 key购买 nike

阅读以下问题和各种答案( "Least Astonishment" and the Mutable Default Argument )以及官方文档( https://docs.python.org/3/tutorial/controlflow.html#default-argument-values )后,我编写了 ResultsClass ,以便它的每个实例都有一个单独的列表,而不影响默认值(至少,根据我新获得的理解,这应该是发生的事情):

class ResultsClass:
def __init__(self,
project = None,
badpolicynames = None,
nonconformpolicydisks = None,
diskswithoutpolicies = None,
dailydifferences = None,
weeklydifferences = None):
self.project = project
if badpolicynames is None:
self.badpolicynames = []
if nonconformpolicydisks is None:
self.nonconformpolicydisks = []
if diskswithoutpolicies is None:
self.diskswithoutpolicies = []
if dailydifferences is None:
self.dailydifferences = []
if weeklydifferences is None:
self.weeklydifferences = []

就其本身而言,这可以按预期工作:

i = 0
for result in results:
result.diskswithoutpolicies.append("count is " + str(i))
print(result.diskswithoutpolicies)
i = i+1
['count is 0']
['count is 1']
['count is 2']
['count is 3']
etc.

此脚本的上下文是我尝试从 Google Cloud 基础架构内的每个项目获取信息;在此实例中,主要是具有与其关联的快照计划的磁盘列表、过去 24 小时内每个磁盘的计划快照列表、具有不符合我们的命名约定的错误计划名称的磁盘,以及根本没有任何与它们关联的快照计划。

在完整的脚本中,我使用了完全相同的 ResultsClass;然而,当在多个 for 循环中使用时,附加似乎再次添加到默认值中,老实说,我不明白为什么。代码的简化版本如下:

# Code to obtain a list of projects
results = [ResultsClass() for i in range((len(projects)))]
for result in results:
for project in projects:
result.project = project
# Code to obtain each zone in the project
for zone in zones:
# Code to get each disk in zone
for disk in disks:
resourcepolicy = disk.get('resourcePolicies')
if resourcepolicy:
# Code to action if a resource policy exists
else:
result.badpolicynames.append(resourcepolicy[0].split('/')[-1])
result.nonconformpolicydisks.append(disk['id'])
else:
result.diskswithoutpolicies.append(disk['id'])
pprint(vars(result))

然后返回结果:

{'badpolicynames': [],
'dailydifferences': None,
'diskswithoutpolicies': ['**1098762112354315432**'],
'nonconformpolicydisks': [],
'project': '**project0**',
'weeklydifferences': None}
{'badpolicynames': [],
'dailydifferences': None,
'diskswithoutpolicies': ['**1098762112354315432**'],
['**1031876156872354739**'],
'nonconformpolicydisks': [],
'project': '**project1**',
'weeklydifferences': None}

一个for循环(或多个for循环)是否会以某种方式否定在ResultsClass中创建的单独列表?我需要了解为什么 Python 中会发生这种情况,然后如何纠正它。

最佳答案

根据我的最佳理解,一个明显的问题是您将结果和项目循环嵌套在一起,而您应该只循环其中的一个。我建议循环项目并在每个项目中创建一个结果,而不是之前实例化列表中的类。

results = []
for project in projects:
result = ResultsClass(project)
# Code to obtain each zone in the project
for zone in zones:
# Code to get each disk in zone
for disk in disks:
resourcepolicy = disk.get('resourcePolicies')
if resourcepolicy:
# Code to action if a resource policy exists
else:
result.badpolicynames.append(resourcepolicy[0].split('/')[-1])
result.nonconformpolicydisks.append(disk['id'])
else:
result.diskswithoutpolicies.append(disk['id'])
results.append(result)
pprint(vars(result))

这样,results 就是您的 ResultsClass 的列表,并且每个 result 仅包含一个项目,而您之前的尝试将以具有相同的最后一个项目的每个 ResultsClass 结束。

关于python - 在多个 For 循环中追加到类列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59323293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com