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

您可以在Python中创建变量列表而不实例化变量吗?,python,list,loops,variables,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_rig

我正在努力使我的代码更干净

我想做一些类似的事情:

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)
我想通过
手势传感器\u数据运行一个循环

有办法做到这一点吗?某种结构还是什么

编辑:我将在这个函数中显示我的完整代码以供上下文使用

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

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

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

您可以在
dict
中收集所有这些变量。键是什么?这似乎是一种合理的方法;这有什么问题吗?我在四行中运行相同的代码,我可以在一个简单的for循环中运行。你不需要这个列表。您可以在itertools.chan([func1(),func2(),func3()])@dirtysocks45上循环:现在好点了吗?这对我当前代码中的逻辑有效吗?我的fill_gyro_和acc_数据返回两个变量。@dirtysocks45:它不返回两个变量。它返回长度为2的iterable。你正在拆包。函数总是只返回一个对象。我们正在使用itertools.chain合并多个iterables,那么如何将本地变量(如
nod\u gyro
)设置为该命令返回的值?nod\u gyro、nod\u acc、swipe\u left\u gyro、swipe\u right\u acc等=itertools.chain([…])另一种在不减少两个反模式的情况下展平结果的方法是:不要使用
reduce
来重新创建
sum
,也不要使用
sum
来展平列表。
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)])