0

Basically I want to group all the dictionaries from the list message with the same key (name) in different lists. How should I proceed?

I was thinking about doing some kind of two loops but nothing seems to work.

messages={('name': FLR345, 'latitude': 34.244, 'longitude': -23.564), ('name': FLR33, 'latitude': 34.24432, 'longitude': -24.53),('name': FLR345, 'latitude': 35.244, 'longitude': -26.564), ('name': FLR31, 'latitude': 30.244, 'longitude': -22.564)}

I want something like this

Drone1={('name': FLR345, 'latitude': 34.244, 'longitude': -23.564),'name': FLR345, 'latitude': 35.244, 'longitude': -26.564)}
Drone2={('name': FLR33, 'latitude': 34.24432, 'longitude': -24.53)}
Drone3={('name': FLR31, 'latitude': 30.244, 'longitude': -22.564)}
def split
for i in range(len(messages))
    for j in range(len(messages))
        if .......
        Dronei=....
Community
  • 1
  • 1
  • 6
    Is `messages` a list or a `set`? And it looks like you are treating `tuples` like dictionaries inside? Can you create a sample input that is actually valid python? – user3483203 Aug 05 '19 at 16:37
  • See: [How do I use Python's itertools.groupby()?](https://stackoverflow.com/questions/773/how-do…) – hugo Aug 05 '19 at 16:48
  • The sample `messages` that you provided is not in a valid format e.g. `('name': FLR345, 'latitude': 34.244, 'longitude': -23.564)` is not valid and `FLR345` needs to be in quotes unless it's an object. Please provide a valid sample of your data. – Trenton McKinney Aug 05 '19 at 16:50
  • messages={('name': 'FLR345', 'latitude': 34.244, 'longitude': -23.564), ('name': 'FLR33', 'latitude': 34.24432, 'longitude': -24.53),('name': FLR345, 'latitude': 35.244, 'longitude': -26.564), ('name': 'FLR31', 'latitude': 30.244, 'longitude': -22.564)} Sorry I forgot the ' '. – Saul Santos Aug 05 '19 at 17:10
  • The data you provided is not a valid. Python will give syntax error at : – amol rane Aug 05 '19 at 17:57

1 Answers1

0

Although I did not completely understand your messages data structure, I made an assumption from your description that it might be a tuple containing dictionaries as elements (rather than the vice versa which is what your question contains). If that is the case, below solution might work, you may have to make a couple changes though, but the idea looks to work fine:

  1. Map distinct names to a group name and form a dictionary out of them like 'FLR345' to 'Drone0', 'FLR33' to 'Drone1' etc
  2. Go through each element in messages tuple and see if the name already has a group assigned
  3. If assigned, just append that message dictionary element to that group
  4. Else add them as new entries

    messages = ({'name': 'FLR345', 'latitude': 34.244, 'longitude': -23.564},
            {'name': 'FLR33', 'latitude': 34.24432, 'longitude': -24.53},
            {'name': 'FLR345', 'latitude': 35.244, 'longitude': -26.564},
            {'name': 'FLR31', 'latitude': 30.244, 'longitude': -22.564})
    messages_grouped = {}
    name_to_message_group = {}
    for i in range(len(messages)):
        #check if the current message name has a group assigned
         if messages[i]['name'] in  name_to_message_group.keys():
             #if yes, append the current message to existing group
             messages_grouped[name_to_message_group[messages[i]['name']]] += (messages[i],)
         else:
             #else add the name to group mapping first
            name_to_message_group[messages[i]['name']] = 'Drone' + str(i)
            #then add the group and message as new elements
            messages_grouped['Drone' + str(i)] = tuple()
            messages_grouped['Drone' + str(i)] += (messages[i],)
    print (messages_grouped)
    
Pavan Tej
  • 71
  • 5