簡體   English   中英

您可以在Python中創建變量列表而無需實例化變量嗎?

[英]Can you create a list of variables in Python without instantiating the 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
itertools.chain([fill_gyro_and_acc_data(i, merge) for i in [
    nod_intervals, swipe_right_intervals, swipe_left_intervals, whats_up_intervals
])

你可以改變你的generate_gesture_intervals並使用partial

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM