Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

Change one dict/list python structure to another

I have the following data

data={
       None: [
               {'ne': '1', 'na': '1'}, 
               {'ne': '2', 'na': '2'}, 
               {'ne': '3', 'na': '3'}, 
               {'ne': '4', 'na': '4'}
             ], 
      'AO': [
               {'ne': '2', 'na': '2'}, 
               {'ne': '6', 'na': '6'}
            ], 
      'NZ': [
               {'ne': '1', 'na': '1'}
            ]
      }

and I want to have a list from it like this:

[
  {'ne': '1', 'na': '1', 'country': [None, 'NZ']},
  {'ne': '2', 'na': '2', 'country': [None, 'AO']},
  {'ne': '3', 'na': '3', 'country': [None]},
  {'ne': '4', 'na': '4', 'country': [None]},
  {'ne': '6', 'na': '6', 'country': ['AO']}
]

my code is doing it fine but it's far from being "pythonic" because I'm a newbie at python:

data = {None: [{'ne': '1', 'na': '1'}, {'ne': '2', 'na': '2'}, {'ne': '3', 'na': '3'}, {'ne': '4', 'na': '4'}], 'AO': [{'ne': '2', 'na': '2'}, {'ne': '6', 'na': '6'}], 'NZ': [{'ne': '1', 'na': '1'}]}
data_list = []
for k,d in data.items():
    for dd in d:
        dd['country'] = k
        data_list.append(dd)
help_dict = {}
for item in data_list:
    help_dict[item['ne']] = False
final_list = []
for idx, val in enumerate(data_list):
    if not help_dict[val['ne']]:
        val['country'] = [val['country']]
        for idx2, val2 in enumerate(data_list):
            if idx2 != idx and val['ne'] == val2['ne']:
                val['country'].append(val2['country'])
        help_dict[val['ne']] = True
        final_list.append(val)
print(final_list)

can someone help me with a better way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
new = [x for key,value in data.items() for x in value]

# remove duplicate dictionaries
new = [dict(t) for t in {tuple(d.items()) for d in new}]

for d in new:
    d['country'] = [key for key,data in data.items() if d in data]

print(new)

>>> [{'ne': '2', 'na': '2', 'country': [None, 'AO']}, 
     {'ne': '4', 'na': '4', 'country': [None]}, 
     {'ne': '1', 'na': '1', 'country': [None, 'NZ']}, 
     {'ne': '6', 'na': '6', 'country': ['AO']}, 
     {'ne': '3', 'na': '3', 'country': [None]}]

If you want to preserve the order

new = [x for n,(key,value) in enumerate(data.items()) for x in value]

seen = set()
new_l = []
for d in new:
    t = tuple(d.items())
    if t not in seen:
        seen.add(t)
        new_l.append(d)

for d in new_l:
    d['country'] = [key for key,data in data.items() if d in data]

print(new_l)

>>> [{'ne': '1', 'na': '1', 'country': [None, 'NZ']}, 
     {'ne': '2', 'na': '2', 'country': [None, 'AO']}, 
     {'ne': '3', 'na': '3', 'country': [None]}, 
     {'ne': '4', 'na': '4', 'country': [None]}, 
     {'ne': '6', 'na': '6', 'country': ['AO']}]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...