将键映射到python目录中的值

时间:2018-07-19 21:24:52

标签: python python-3.x directory key

我有一个目录:

ca={'currency': ["-RMB"],
 'Country': ['‐China,
  '‐Japan',
  '‐USA',
  '‐UK',
  '‐Mexico'],
 'Languages':["-Chinese","-Japanese","-English","English","Spanish"]}

我只想打印出密钥,使其成为一个数组:看起来像:

["Currency","Country","Country","Country","Country","Languages","Languages","Languages","Languages","Languages"],

我尝试了几种方法,例如访问每种方法:

ca.["Country"].replace("Country"),我知道语法错误,但这是我的逻辑。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您可以使用列表推导来获取与值相同次数的键:

[x for x,y in zip(ca.keys(),ca.values()) for i in y]

['currency', 'Country', 'Country', 'Country', 'Country', 'Country', 'Languages', 'Languages', 'Languages', 'Languages', 'Languages']