提问人:Pm740 提问时间:11/15/2023 最后编辑:Pm740 更新时间:11/15/2023 访问量:74
为什么 dict 理解不起作用,而 for 循环不起作用?[复制]
Why does dict comprehension doesn't works when for-loops do? [duplicate]
问:
调用本模块中的示例函数时,为什么会抛出异常
当我使用推导式(可以通过参数切换)时?有人可以解释一下例外的含义吗?似乎被覆盖了,我无法将我的头包裹起来。func
cycle
与循环和理解具有相同功能的示例函数
import pandas as pd
def func(
x_dict,
keys_list,
start_cycle,
end_cycle,
comprehension=True
):
x_test_dicts = {}
for cycle in range(start_cycle, end_cycle + 1):
print(f"cycle = {cycle}")
if comprehension:
# Fill the dict with comprehension.
x_test_dict = {
f"{key}_input":
x_dict[key].query('cycle == @cycle').values
for key in keys_list
}
else:
# Fill the dict with normal for loop.
x_test_dict = {}
for key in keys_list:
x_test_dict[f"{key}_input"] = \
x_dict[key].query('cycle == @cycle').values
x_test_dicts[cycle] = x_test_dict
return x_test_dicts
创建测试数据
import pandas as pd
import numpy as np
# Create an ID array from 1 to 1000
ids = np.arange(1, 1001)
# Calculate cycle as ID divided by 100
cycles = ids // 100
# Generate random integer values for the remaining columns
# Assuming a range for random integers (e.g., 0 to 100)
col1_int = np.random.randint(0, 101, 1000)
col2_int = np.random.randint(0, 101, 1000)
col3_int = np.random.randint(0, 101, 1000)
# Update the DataFrame with integer values
df = pd.DataFrame({
"ID": ids,
"cycle": cycles,
"col1": col1_int,
"col2": col2_int,
"col3": col3_int
})
df.head() # Display the first few rows of the updated DataFrame
使用函数运行测试用例
import pandas as pd
df = df.set_index(['ID', 'cycle']) # Use multi-indexing
x_dict = {'Auxin': df} # Create a simple dict with the DataFrame
keys_list = ['Auxin'] # Define a list of keys to work with
# Define ranges for the loop inside `func`
start_cycle = 6
end_cycle = 29
# RUNS SUCCESSFULLY WITHOUT LIST COMPREHENSION
comprehension = False
result = func(
x_dict,
keys_list,
start_cycle,
end_cycle,
comprehension=comprehension
)
print("Worked without dict comprehension!")
# FAILS WITH LIST COMPREHENSION
comprehension = True
result = func(
x_dict,
keys_list,
start_cycle,
end_cycle,
comprehension=comprehension
)
print("Breaks when dict comprehension is used!")
错误
UndefinedVariableError: local variable 'cycle' is not defined
答:
1赞
Daraan
11/15/2023
#1
简而言之,理解的工作方式与人们预期的不同。它们对未公开的局部变量使用不同的范围。一些阅读在这里: https://peps.python.org/pep-0572/#changing-the-scope-rules-for-comprehensions
这里的 pandas 和 comprehension 不共享相同的范围,当尝试通过 comprehension 中的 pandas 访问它时,没有定义这一点。cycle
- 您可以在 for 循环之前使用它来允许访问 .
global cycle
cycle
- 第二种方式看起来有点奇怪,但进入了理解范围。也许在你的真实例子中,你可以用更优雅的方式做到这一点
cycle
import pandas as pd
def func(
x_dict,
keys_list,
start_cycle,
end_cycle,
comprehension=True
):
x_test_dicts = {}
for cycle in range(start_cycle, end_cycle + 1):
print(f"cycle = {cycle}")
if comprehension:
# Fill the dict with comprehension.
x_test_dict = {
f"{key}_input":
(cycle := cycle, # defines cycle in the local scope
x_dict[key].query('cycle == @cycle').values)[1] # access element you want
for key in keys_list
}
else:
# Fill the dict with normal for loop.
x_test_dict = {}
for key in keys_list:
x_test_dict[f"{key}_input"] = \
x_dict[key].query('cycle == @cycle').values
x_test_dicts[cycle] = x_test_dict
return x_test_dicts
评论