将foo添加到字典键

时间:2015-11-07 02:27:20

标签: python dictionary

如何将[same] foo添加到字典的所有键中,以便

a = {'continent': ['America', 'South Africa'], 'country': ['USA', 'Egypt']}

变为:

a = {'foo(bar=continent, hello=world)': ['America', 'South Africa'], 'foo(bar=country, hello=world)': ['USA', 'Egypt']}

编辑: 我实际上是想将str键编辑为非str键,所以:

a = {foo(bar='continent', hello=world): ['America', 'South Africa'], foo(bar='country', hello=world): ['USA', 'Egypt']}

1 个答案:

答案 0 :(得分:4)

a = {'foo(bar=' + key + ', hello=world)': a[key] for key in a.keys()}