自动将变量名称传递给列表
Automatically pass variables names to list
对于脚本的一部分,我有很长的代码链。例如:
B1_2013 = images.select('0_B1')
B2_2013 = images.select('0_B2')
B3_2013 = images.select('0_B3')
B4_2013 = images.select('0_B4')
B5_2013 = images.select('0_B5')
B6_2013 = images.select('0_B6')
B7_2013 = images.select('0_B7')
B8_2013 = images.select('0_B8')
B9_2013 = images.select('0_B9')
B10_2013 = images.select('0_B10')
B11_2013 = images.select('0_B11')
B1_2014 = images.select('1_B1')
B2_2014 = images.select('1_B2')
B3_2014 = images.select('1_B3')
B4_2014 = images.select('1_B4')
B5_2014 = images.select('1_B5')
B6_2014 = images.select('1_B6')
B7_2014 = images.select('1_B7')
B8_2014 = images.select('1_B8')
B9_2014 = images.select('1_B9')
B10_2014 = images.select('1_B10')
B11_2014 = images.select('1_B11')
and so on ...
B11_2020 = images.select('7_B11')
最终,我需要根据这些代码行生成一个变量列表 (my_variables
),我可以将其传递给函数。例如:
my_variables = [B1_2013, B2_2013, B3_2013, B4_2013, B5_2013, B6_2013, B7_2013, B8_2013, B9_2013, B10_2013, B11_2013, \
B1_2014, B2_2014, B3_2014, B4_2014, B5_2014, B6_2014, B7_2014, B8_2014, B9_2014, B10_2014, B11_2014]
有没有一种更有效的方法可以在第一个代码示例之后自动生成数百行代码(例如B1_2013 = images.select('0_B1') and so on...
),以便我可以在第二个代码示例中自动生成变量列表(例如my_variables = [B1_2013, and so on...]
?
只需使用循环或列表理解来制作列表。
my_images = [images.select(f'{i}_B{j}') for i in range(8) for j in range(12)]
@Barmar 的回答是正确的。如果你想通过执行以下操作来索引变量,你可以扩展他的答案:
my_images = {f'{i}_B{j}':images.select(f'{i}_B{j}') for i in range(8) for j in range(12)}
这叫做字典理解。
或字典理解:
my_images = {'B{j}_{2013 + i}': images.select(f'{i}_B{j}') for i in range(8) for j in range(12)}
推荐他们:
my_images['B1_2013']
my_images['B2_2013']
...
在这个用例中,使用 dict
来存储“变量”更为可行,动态构建变量不是好的做法。下面是一个使用 itertools.product
的示例,它将构建一个具有所需输出的 dict
:
from itertools import product
images = {f'B{i}_{y+2013}': images.select(f'{y}_B{i}')
for y, i in product(range(12), range(1, 8))}
结果:
{'B1_2013': '0_B1',
'B2_2013': '0_B2',
'B3_2013': '0_B3',
'B4_2013': '0_B4',
'B5_2013': '0_B5',
'B6_2013': '0_B6',
'B7_2013': '0_B7',
'B1_2014': '1_B1',
'B2_2014': '1_B2',
'B3_2014': '1_B3',
...
}
然后访问所需的“变量”使用:
>>> images['B3_2014']
'1_B1'
对于脚本的一部分,我有很长的代码链。例如:
B1_2013 = images.select('0_B1')
B2_2013 = images.select('0_B2')
B3_2013 = images.select('0_B3')
B4_2013 = images.select('0_B4')
B5_2013 = images.select('0_B5')
B6_2013 = images.select('0_B6')
B7_2013 = images.select('0_B7')
B8_2013 = images.select('0_B8')
B9_2013 = images.select('0_B9')
B10_2013 = images.select('0_B10')
B11_2013 = images.select('0_B11')
B1_2014 = images.select('1_B1')
B2_2014 = images.select('1_B2')
B3_2014 = images.select('1_B3')
B4_2014 = images.select('1_B4')
B5_2014 = images.select('1_B5')
B6_2014 = images.select('1_B6')
B7_2014 = images.select('1_B7')
B8_2014 = images.select('1_B8')
B9_2014 = images.select('1_B9')
B10_2014 = images.select('1_B10')
B11_2014 = images.select('1_B11')
and so on ...
B11_2020 = images.select('7_B11')
最终,我需要根据这些代码行生成一个变量列表 (my_variables
),我可以将其传递给函数。例如:
my_variables = [B1_2013, B2_2013, B3_2013, B4_2013, B5_2013, B6_2013, B7_2013, B8_2013, B9_2013, B10_2013, B11_2013, \
B1_2014, B2_2014, B3_2014, B4_2014, B5_2014, B6_2014, B7_2014, B8_2014, B9_2014, B10_2014, B11_2014]
有没有一种更有效的方法可以在第一个代码示例之后自动生成数百行代码(例如B1_2013 = images.select('0_B1') and so on...
),以便我可以在第二个代码示例中自动生成变量列表(例如my_variables = [B1_2013, and so on...]
?
只需使用循环或列表理解来制作列表。
my_images = [images.select(f'{i}_B{j}') for i in range(8) for j in range(12)]
@Barmar 的回答是正确的。如果你想通过执行以下操作来索引变量,你可以扩展他的答案:
my_images = {f'{i}_B{j}':images.select(f'{i}_B{j}') for i in range(8) for j in range(12)}
这叫做字典理解。
或字典理解:
my_images = {'B{j}_{2013 + i}': images.select(f'{i}_B{j}') for i in range(8) for j in range(12)}
推荐他们:
my_images['B1_2013']
my_images['B2_2013']
...
在这个用例中,使用 dict
来存储“变量”更为可行,动态构建变量不是好的做法。下面是一个使用 itertools.product
的示例,它将构建一个具有所需输出的 dict
:
from itertools import product
images = {f'B{i}_{y+2013}': images.select(f'{y}_B{i}')
for y, i in product(range(12), range(1, 8))}
结果:
{'B1_2013': '0_B1',
'B2_2013': '0_B2',
'B3_2013': '0_B3',
'B4_2013': '0_B4',
'B5_2013': '0_B5',
'B6_2013': '0_B6',
'B7_2013': '0_B7',
'B1_2014': '1_B1',
'B2_2014': '1_B2',
'B3_2014': '1_B3',
...
}
然后访问所需的“变量”使用:
>>> images['B3_2014']
'1_B1'