英文:
.items() reading a dictionary as a list and throwing an attribute error
问题
我正在使用IN-CORE,这是一个用于自然灾害分析的平台。我正在使用他们的库(pyincore)中的蒙特卡洛定义。我正在使用以下代码来遍历不同基础设施位置的损伤状态字典条目列表。
failure = []
for i in range(10):
print(damages[i]) # 打印 {'DS_0': Decimal('0.6716138851'), 'DS_1': 0, 'DS_2': 0, 'DS_3': Decimal('0.3283861149')}
print(damages[i].items()) # 打印 dict_items([('DS_0', Decimal('0.6716138851')), ('DS_1', 0), ('DS_2', 0), ('DS_3', Decimal('0.3283861149'))])
damage_interval_keys = ["DS_0" , "DS_3"]
failure_state_keys = ["DS_3"]
(MonteCarloFailureProbability.calc_probability_failure_value(damages[i], damage_interval_keys, failure_state_keys))
print(failure)
然而,我遇到了以下错误:
AttributeError Traceback (most recent call last)
Cell In[167], line 9
7 damage_interval_keys = ["DS_0" , "DS_3"]
8 failure_state_keys = ["DS_3"]
----> 9 (MonteCarloFailureProbability.calc_probability_failure_value(damages[i], damage_interval_keys, failure_state_keys))
11 print(failure)
File ~\AppData\Local\miniconda3\envs\pyincoreEnv\lib\site-packages\pyincore\analyses\montecarlofailureprobability\montecarlofailureprobability.py:329, in MonteCarloFailureProbability.calc_probability_failure_value(self, ds_sample, failure_state_keys)
327 count = 0
328 func = {}
--> 329 for sample, state in ds_sample.items():
330 if state in failure_state_keys:
331 func[sample] = "0"
AttributeError: 'list' object has no attribute 'items'
我确保了字典条目列表没有嵌套在另一个列表中,但正如上面的代码所示,.items() 在函数调用外部是有效的,并且 .dtype 返回列表在某个索引处是一个字典。我感到非常困惑,所以任何帮助都将是极好的。
英文:
I am using IN-CORE, a analysis platform for natural disasters. I am working with a Monte Carlo definition in their library (pyincore). I am using this code to run through a list of dictionary entries of damage states for different infrastructure locations.
failure = []
for i in range(10):
print(damages[i]) # prints {'DS_0': Decimal('0.6716138851'), 'DS_1': 0, 'DS_2': 0, 'DS_3': Decimal('0.3283861149')}
print(damages[i].items()) # prints dict_items([('DS_0', Decimal('0.6716138851')), ('DS_1', 0), ('DS_2', 0), ('DS_3', Decimal('0.3283861149'))])
damage_interval_keys = ["DS_0" , "DS_3"]
failure_state_keys = ["DS_3"]
(MonteCarloFailureProbability.calc_probability_failure_value(damages[i], damage_interval_keys, failure_state_keys))
print(failure)
However, I get this error:
AttributeError Traceback (most recent call last)
Cell In[167], line 9
7 damage_interval_keys = ["DS_0" , "DS_3"]
8 failure_state_keys = ["DS_3"]
----> 9 (MonteCarloFailureProbability.calc_probability_failure_value(damages[i], damage_interval_keys, failure_state_keys))
11 print(failure)
File ~\AppData\Local\miniconda3\envs\pyincoreEnv\lib\site-packages\pyincore\analyses\montecarlofailureprobability\montecarlofailureprobability.py:329, in MonteCarloFailureProbability.calc_probability_failure_value(self, ds_sample, failure_state_keys)
327 count = 0
328 func = {}
--> 329 for sample, state in ds_sample.items():
330 if state in failure_state_keys:
331 func[sample] = "0"
AttributeError: 'list' object has no attribute 'items'
I made sure the list of the dict entries wasn't layered in another list, but as the code above prints, the .items() works outside of the function call and the .dtype returns that the list at a certain index is a dictionary. I am quite confounded, so any help would be amazing.
答案1
得分: 0
由于您在类上调用方法而不是实例上调用,因此第一个参数被用作 self
,而不是 ds_sample
,并且列表 damage_interval_keys
被用作 ds_sample
。
首先创建一个类的实例,然后在该实例上调用方法。
sim = MonteCarloFailureProbability()
failure = []
for i in range(10):
print(damages[i]) # 打印 {'DS_0': Decimal('0.6716138851'), 'DS_1': 0, 'DS_2': 0, 'DS_3': Decimal('0.3283861149')}
print(damages[i].items()) # 打印 dict_items([('DS_0', Decimal('0.6716138851')), ('DS_1', 0), ('DS_2', 0), ('DS_3', Decimal('0.3283861149'))])
damage_interval_keys = ["DS_0" , "DS_3"]
failure_state_keys = ["DS_3"]
(sim.calc_probability_failure_value(damages[i], damage_interval_keys, failure_state_keys))
print(failure)
然而,还有其他问题 -- 您在最后打印 failure
,但没有更新它。您是否遗漏了一个将调用结果添加到 failure
的调用?
英文:
Since you're calling the method on the class, rather than an instance, the first argument is being used as self
, not ds_sample
, and the list damage_interval_keys
is being used as ds_sample
.
Create an instance of the class first, and call the method on that.
sim = MonteCarloFailureProbability()
failure = []
for i in range(10):
print(damages[i]) # prints {'DS_0': Decimal('0.6716138851'), 'DS_1': 0, 'DS_2': 0, 'DS_3': Decimal('0.3283861149')}
print(damages[i].items()) # prints dict_items([('DS_0', Decimal('0.6716138851')), ('DS_1', 0), ('DS_2', 0), ('DS_3', Decimal('0.3283861149'))])
damage_interval_keys = ["DS_0" , "DS_3"]
failure_state_keys = ["DS_3"]
(sim.calc_probability_failure_value(damages[i], damage_interval_keys, failure_state_keys))
print(failure)
There's other problems, though -- you're printing failure
at the end, but nothing updates that. Are you missing a call to failure.append()
with the results of the call?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论