如何在与相同键关联的计划字符串中获取字典值

时间:2019-03-07 12:49:08

标签: python-3.x dictionary pytest

我有字典作为输入,例如

输入词典:

{'location': {'street1': 'Deborah Throughway', 'city': 'East Betty', 'stateOrProvince': 'AK', 'postcode': '50545', 'country': 'US'}}

预期输出为:

'SiteAddress': 'Deborah Throughway, East Betty, AK, US, 50545'

我尝试了list(dictionary['location'].values()),但得到

{'SiteAddress': ['Deborah Throughway', 'East Betty', 'AK', '50545', 'US'] }

请提出我在做错什么以及如何获得预期的输出

1 个答案:

答案 0 :(得分:0)

data = {'location': {'street1': 'Deborah Throughway', 'city': 'East Betty', 'stateOrProvince': 'AK', 'postcode': '50545', 'country': 'US'}}

def main():
    loc = data['location']
    address = ",".join([loc['street1'], loc['city'], loc['stateOrProvince'], loc['country'], loc['postcode']])

    print("SiteAddress: {0}".format(address))


if __name__ == '__main__':
    main()

输出:

SiteAddress: Deborah Throughway,East Betty,AK,US,50545