Python - Append particular numpy arrays based on value -
i trying find shorter way write code.
i have 20 folders, each different value in name (1 - 20).
in each folder there text files list of numbers in them. apply function each of these lists , append numpy list, 1 of 20, corresponding number in folder name.
i trying find way append correct numpy array without having have 20 'if...else if' statements checking folder number.
this code feels unnecessarily long , hoping more concise way it.
-------- current psudocode ------
array_one = [] array_two = [] ... if folder_number == 1: array_one.append(list_from_folder) elif folder_number == 2: array_two.append(list_from_folder) ...
any recommendations? (using python)
use dictionary:
folder_lists = {num: [] num in range(1, 21)}
this give key: value mapping data structure in keys (e.g. 1, 2, 3, etc.) correspond folder numbers , values lists map keys (those folders):
{1: [], 2: [], 3: [], ..., 20: []}
then this:
for f in folders: folder_number = <insert folder number here> folder_lists[folder_number].append(list_from_folder)
Comments
Post a Comment