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)

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

How do I remove duplicates? i.e. id4 to only display Username1 once
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'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.