python - 您可以在不实例化变量的情况下在 Python 中创建变量列表吗?

标签 python list loops variables

我正在努力让我的代码更简洁。

我想做这样的事情:

gesture_sensor_data = [nod_gyro, nod_acc, swipe_left_gyro, swipe_right_acc, etc.]

我现在有这个:

nod_gyro, nod_acc = fill_gyro_and_acc_data(nod_intervals, merge)
swipe_right_gyro, swipe_right_acc = fill_gyro_and_acc_data(swipe_right_intervals, merge)
swipe_left_gyro, swipe_left_acc = fill_gyro_and_acc_data(swipe_left_intervals, merge)
whats_up_gyro, whats_up_acc = fill_gyro_and_acc_data(whats_up_intervals, merge)

我想通过 gesture_sensor_data 运行一个循环。

有没有办法做到这一点?某种结构之类的?

编辑:我将在此函数中显示我的完整代码作为上下文。

def generate_gesture_files(i):
    nod_intervals, swipe_left_intervals, swipe_right_intervals, whats_up_intervals = generate_gesture_intervals(i)

    merge = pandas.read_csv(final_user_study_path + "/P" + str(i) + "/DataCollection/data/merge.csv")
    nod_gyro, nod_acc = fill_gyro_and_acc_data(nod_intervals, merge)
    swipe_right_gyro, swipe_right_acc = fill_gyro_and_acc_data(swipe_right_intervals, merge)
    swipe_left_gyro, swipe_left_acc = fill_gyro_and_acc_data(swipe_left_intervals, merge)
    whats_up_gyro, whats_up_acc = fill_gyro_and_acc_data(whats_up_intervals, merge)
    return nod_gyro, nod_acc, swipe_right_gyro, swipe_right_acc, swipe_left_gyro, swipe_right_acc, whats_up_gyro, whats_up_acc

最佳答案

你可以改变你的generate_gesture_intervals并使用部分

def generate_gesture_files(i):
  return reduce(lambda x,y:x+y, [fill_gyro_and_acc_data(arg, merge) for arg in generate_gesture_intervals(i)])

关于python - 您可以在不实例化变量的情况下在 Python 中创建变量列表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46771388/

相关文章:

python - 如何扩展使用 AbstractBaseUser 创建的现有自定义用户模型以拥有多种用户类型,例如客户、培训师和作者?

c# - 结合分组和排序的 LINQ 查询

java - 数组中 int 元素的随机整数

c - 使用 for 循环逐行打印,然后在 c 中扫描到另一个

python - 隐含波动率计算器是错误的

python - Django 从 1.9 降级到 1.8

Python 使用随机/选择仅输出一次字符串

python - while循环不会停止循环

JavaScript:.forEach() 和 .map() 之间的区别

python - 如何在 Sklearn 中 reshape 我的测试数据? (特征选择)