Code Monkey
Code Monkey

Reputation: 844

Organizing & sorting columns into dictionary variable

I have a stats.csv file with multiple columns, the first 2 columns of which are (note: I have not included the rest of the table as it contains confidential data):

USER        TEAM

UKW1G1KK8   TFCARKGN5

UL3DTLGRE   TFCARKGN5

UL2HHLQRY   TFCARKGN5

ULTM136EB   TFCARKGN5

UFD51MS69   TFCARKGN6

UKM4K5DJR   TFCARKGN6

UMS5G3PEH   TFCARKGN6

UL7RL2X5E   TFCARKGN6

UMP69CC69   TFCARKGN6

I wish to create a dictionary like this: current_teams = {team: [users]}.

In the above example it would be

current_teams = {TFCARKGN5:[UKW1G1KK8,UL3DTLGRE,UL2HHLQRY,ULTM136EB],  TFCARKGN6:[UFD51MS69,UKM4K5DJR,UMS5G3PEH,UL7RL2X5E,UMP69CC69]}

I wrote this

import csv
stats = {}
with open('Data/stats.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader: stats.update({row['USER']: row})

current_teams = {}
members = []
for user_id, team in zip(list(stats.keys()), [stats[user]['TEAM'] for user in list(stats.keys())]):
    current_teams = {team:{'members':members.append(user_id)}}

But the output I get is this: {'TFCARKGN5': {'members': None}}

Where am I going wrong?

Upvotes: 2

Views: 65

Answers (3)

Code Monkey
Code Monkey

Reputation: 844

Actually, the simplest and most Pythonic way of doing this was staring at me all along! You don't need to import additional modules (other than csv). Just do this:

import csv
stats = {}
with open('Data/stats.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader: stats.update({row['USER']: row})

current_teams = {}
for user_id in list(stats.keys()): 
    if stats[user_id]['TEAM'] in current_campaign: current_campaign[stats[user_id]['TEAM']]['USER'].append(user_id)
    else: 
        current_campaign.update({stats[user_id]['TEAM']:{'USER':[]}})
        current_campaign[stats[user_id]['TEAM']]['USER'].append(user_id)

Upvotes: 0

milanbalazs
milanbalazs

Reputation: 5329

You can use the following code. I have use a txt file instead of csv. You can read the csv file with csv Python module. Probably this site can help: https://realpython.com/python-csv/

test.txt

UKW1G1KK8   TFCARKGN5

UL3DTLGRE   TFCARKGN5

UL2HHLQRY   TFCARKGN5

ULTM136EB   TFCARKGN5

UFD51MS69   TFCARKGN6

UKM4K5DJR   TFCARKGN6

UMS5G3PEH   TFCARKGN6

UL7RL2X5E   TFCARKGN6

UMP69CC69   TFCARKGN6

Code:

result_dict = {}
with open('test.txt') as f:
    for data in f.readlines():
        data = data.strip()
        if not data:
            continue
        if data.split()[1] in result_dict:
            result_dict[data.split()[1]].append(data.split()[0])
            continue
        result_dict[data.split()[1]] = [data.split()[0]]
print(result_dict)

Output:

>>> python3 test.py 
{'TFCARKGN5': ['UKW1G1KK8', 'UL3DTLGRE', 'UL2HHLQRY', 'ULTM136EB'], 'TFCARKGN6': ['UFD51MS69', 'UKM4K5DJR', 'UMS5G3PEH', 'UL7RL2X5E', 'UMP69CC69']}

Upvotes: 0

Ralf
Ralf

Reputation: 16515

It is not clear to me what you want to achieve, but I see that you use members.append(user_id) as the value in your dict, but .append() does not return a value.

I think this code achieves the grouping you are looking for (I read it as a text file, but you can also use the csv module if you prefer):

import collections
teams = collections.defaultdict(set)

with open('test.txt') as f:
    for i, line in enumerate(f):
        if i == 0:
            # skip first line
            continue

        line = line.strip()
        if len(line) > 0:
            user, team = line.split(',')
            teams[team].add(user)

print(teams)
for t, user_set in teams.items():
    print(t, user_set)

The output is:

defaultdict(<class 'set'>, {'TFCARKGN5': {'ULTM136EB', 'UKW1G1KK8', 'UL3DTLGRE', 'UL2HHLQRY'}, 'TFCARKGN6': {'UFD51MS69', 'UMS5G3PEH', 'UKM4K5DJR', 'UMP69CC69', 'UL7RL2X5E'}})
TFCARKGN5 {'ULTM136EB', 'UKW1G1KK8', 'UL3DTLGRE', 'UL2HHLQRY'}
TFCARKGN6 {'UFD51MS69', 'UMS5G3PEH', 'UKM4K5DJR', 'UMP69CC69', 'UL7RL2X5E'}

Upvotes: 1

Related Questions