簡體   English   中英

如何使用python在字典中創建列表

[英]How to create the list inside the dictionary using python

我正在嘗試使用 Boto3 在 quicksight 中自動創建數據集。 但我在某些方面被困住了。 請任何人幫助解決這個問題。 這是我的代碼:

qs = boto3.client('quicksight')

response = qs.describe_data_set(
    AwsAccountId='xxxxxxxx',
    DataSetId='testdatasetv4'
)

columns =response['DataSet']['PhysicalTableMap']['string']['RelationalTable']['InputColumns']

for dic in columns:
    for key in dic:
        print({dic[key]})

我需要這樣的輸出:

response1 = Client.create_data_set(
    AwsAccountId=data['AwsAccountId1'],
    DataSetId=data['DatasetId'],
    Name='testdataset',
    PhysicalTableMap={
        'string': {
            'RelationalTable': {
                'DataSourceArn':response['Arn'],
                'Schema': 'public',
                'Name': 'sales',
             
            
                'InputColumns': [
                    {
                        'Name': 'salesid',
                        'Type': 'INTEGER'
                    },
                    {
                        'Name': 'listid',
                        'Type': 'INTEGER'
                    },
                     {
                        'Name': 'sellerid',
                        'Type': 'INTEGER'
                    },
                     {
                        'Name': 'buyerid',
                        'Type': 'INTEGER'
                    },
                     {
                        'Name': 'eventid',
                        'Type': 'INTEGER'
                    },
                    {
                        'Name': 'dateid',
                        'Type': 'INTEGER'
                    },
                    {
                        'Name': 'qtysold',
                        'Type': 'INTEGER'
                    },
                    {
                        'Name': 'pricepaid',
                        'Type': 'DECIMAL'
                    },
                      
                    {
                        'Name': 'commission',
                        'Type': 'DECIMAL'
                    },     
                    
                    {
                        'Name': 'saletime',
                        'Type': 'DATETIME'
                    },
                ]
            }
        }
    },

如何通過代碼添加上述輸入列。 我能夠提取輸入列,但我不知道添加輸入列。 請幫我做這件事。

這是創建字典並添加不同嵌套元素的示例。 您需要適應解決方案。

columns = ['key1', 'key2', 'key3']
vals = ['1', '2', '3']

mydict = {}
mydict['firstkey'] = 1
mydict['anotherkey'] = {}
mydict['anotherkey']['secondkey'] = 2
mydict['needalist'] = {}
mydict['needalist']['mylist'] = [{k:vals[i]} for i, k in enumerate(columns)]
mydict

{'firstkey': 1,
 'anotherkey': {'secondkey': 2},
 'needalist': {'mylist': [{'key1': '1'}, {'key2': '2'}, {'key3': '3'}]}}

暫無
暫無

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

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