您可以在Python中创建变量列表而无需实例化变量吗?

Can you create a list of variables in Python without instantiating the variables?

我正在努力使我的代码更清晰。

我想做如下的事情:

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

我现在有这个:

1
2
3
4
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运行一个循环。

有办法吗?某种结构还是什么?

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

1
2
3
4
5
6
7
8
9
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


1
2
3
itertools.chain([fill_gyro_and_acc_data(i, merge) for i in [
    nod_intervals, swipe_right_intervals, swipe_left_intervals, whats_up_intervals
])


您可以更改生成手势的时间间隔并使用部分

1
2
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)])