简体   繁体   中英

Iterating over each element of a list inside of a dictionary

I have a dictionary, and have assigned multiple values to it like so -

d = {"names[]": ["System/CPU/User/percent", "System/CPU/System/percent"], "values[]": "average_value"}

I want the output to look like this -

names[]: System/CPU/User/percent
names[]: System/CPU/System/percent
values[]: average_value

How can I accomplish this? The different for loop iterations that I have tried are not correctly parsing through the list.

Thanks.

In [2]: for k, v in d.iteritems():
   ...:     if isinstance(v, list):
   ...:         for s in v:
   ...:             print '{}: {}'.format(k, s)
   ...:     else:
   ...:         print '{}: {}'.format(k, v)
   ...:
values[]: average_value
names[]: System/CPU/User/percent
names[]: System/CPU/System/percent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM