Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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']}]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...