Bijan February 2016

Python Group Array by Column and Display Unique Values

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)

Answers


Andriy Ivaneyko February 2016

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)


Aliminator February 2016

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

http://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'))

Post Status

Asked in February 2016
Viewed 1,302 times
Voted 8
Answered 2 times

Search




Leave an answer