2

I have an Array of Arrays with following format:

x = [["Username1","id3"],
["Username1", "id4"],
["Username1", "id4"],
["Username3", "id3"]]

I want to group by the ids and display all the unique usernames How would I get an output that is like:

id3: Username1, Username3

id4: Username1

Edit: Was able to group by second column but I cannot only display unique values. Here is my code:

data={}
for key, group in groupby(sorted(x), key=lambda x: x[1]):
    data[key]=[v[0] for v in group]
print(data)
Community
  • 1
  • 1
Bijan
  • 7,737
  • 18
  • 89
  • 149

2 Answers2

1

Use dict to create unique keys by id and pythons sets to store values ( so you would store only unique names for that keys):

items = [
    ["Username1","id3"],
    ["Username1", "id4"],
    ["Username1", "id4"],
    ["Username3", "id3"]
]

data = {}
for item in items:
    if data.has_key(item[1]):
        data[item[1]].add(item[0])
    else:
        data[item[1]] = set([item[0]])
print(data)
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
0

You may use a for loop but using a linq statement might be cleaner for future usage.

https://stackoverflow.com/a/3926105/4564614

has some great ways to incorpurate linq to solve this issue. I think what you are looking for would be grouping by.

Example:

from collections import defaultdict
from operator import attrgetter

   def group_by(iterable, group_func):
       groups = defaultdict(list)
       for item in iterable:
           groups[group_func(item)].append(item)
       return groups

   group_by((x.foo for x in ...), attrgetter('bar'))
Community
  • 1
  • 1
Aliminator
  • 99
  • 6