此问题在此处已有答案:
Can't use locals() in list comprehension in Python 3?(1个答案)
1小时前关闭
当调用这个模块中的示例函数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条答案
按热度按时间5uzkadbs1#
简而言之,解析在底层的工作方式与人们预期的不同。它们对底层的局部变量使用了不同的作用域,这些作用域没有公开。https://peps.python.org/pep-0572/#changing-the-scope-rules-for-comprehensions
这里pandas和comprehension不共享相同的作用域,当试图通过pandas在comprehension中访问它时,这个
cycle
没有定义。1.修复您可以使用它在for循环之前使用
global cycle
以允许访问cycle
。1.第二种方法看起来有点奇怪,但是将
cycle
移动到了理解作用域。字符串