如何在Python中动态生成字典数组?
我使用boto3 API自动将分区添加到粘合表中。为了创建一个单独的分区,我可以使用create_partition api,它要求我指定一个字典作为输入,如下所示如何在Python中动态生成字典数组?,python,arrays,python-2.7,dictionary,boto3,Python,Arrays,Python 2.7,Dictionary,Boto3,我使用boto3 API自动将分区添加到粘合表中。为了创建一个单独的分区,我可以使用create_partition api,它要求我指定一个字典作为输入,如下所示 PartitionInput = { 'Values': [ '2018', '08', '25', '06' ], 'Storage
PartitionInput = {
'Values': [
'2018', '08', '25', '06'
],
'StorageDescriptor': {
'Location': 'some_location/2018/08/25/06',
'InputFormat': 'input_format',
'OutputFormat': 'output_format',
'SerdeInfo': 'serde_info'
}
}
现在,我想使用batch_create_partition API,在这里我需要指定上面的dict数组,以便一起创建多个分区。因此,如果用户输入2018年8月25日(开始日期)和3作为分区数,那么我的数组应该包含3个值,其中每个值都是上面的dict,但其值和位置会发生变化。因此,输出将为-
PartitionInput = [{
'Values': [
'2018', '08', '25', '00'
],
'StorageDescriptor': {
'Location': 'some_location/2018/08/25/06',
'InputFormat': 'input_format',
'OutputFormat': 'output_format',
'SerdeInfo': 'serde_info'
}
},
{
'Values': [
'2018', '08', '25', '01'
],
'StorageDescriptor': {
'Location': 'some_location/2018/08/25/06',
'InputFormat': 'input_format',
'OutputFormat': 'output_format',
'SerdeInfo': 'serde_info'
}
}, {
'Values': [
'2018', '08', '25', '03'
],
'StorageDescriptor': {
'Location': 'some_location/2018/08/25/06',
'InputFormat': 'input_format',
'OutputFormat': 'output_format',
'SerdeInfo': 'serde_info'
}
}
]
我对python和编程都是新手,所以我不知道该怎么做。我能够使用以下代码解决上述问题-
for x in range(24):
year = 2018
month = 02
day = 25
hour = x
part_loc = "some_location/{}/{}/{}/{}".format(year, month, day, hour)
input_dict = {
'Values': [
str(year), str(month), str(day), str(hour)
],
'StorageDescriptor': {
'Location': part_loc,
'InputFormat': 'input_format',
'OutputFormat': 'output_format',
'SerdeInfo': 'serde_info'
}
}
input_list.append(input_dict.copy())
print input_list