work_in_progress
work_in_progress

Reputation: 767

Extract specific column and group them from dictionary in Python

I want to extract specific columns and group them from the records which I get using MySQLdb. I have written following code:

import _mysql
cdb=_mysql.connect(host="myhost",user="root",
                  passwd="******",db="my_db")
qry = "select col1,col2,col3,col4,col5,col6 from mytable"

cdb.query(qry)

resultset = cdb.store_result()

records = resultset.fetch_row(0,1) # 0 - no limit, 1 - output is in dictionary form

I want to extract only 3 columns: col1, col3 and col4 from the records and want to make groups of unique values using these three columns i.e. all unique combinations of (col1,col3,col4). I know I have to use set() datatype to find unique values and I tried to used it but I din't find any success. Let me know what will be the good solution for it.

I have thousand of records in the database. I am getting the output of records in following way:

({
    'col1':'data11',
    'col2':'data11',
    'col3':'data13',
    'col4':'data14',
    'col5':'data15',
    'col6':'data16'
},
{
    'col1':'data21',
    'col2':'data21',
    'col3':'data23',
    'col4':'data24',
    'col5':'data25',
    'col6':'data26'
})

Upvotes: 0

Views: 615

Answers (1)

Rusty
Rusty

Reputation: 914

I have come up with this solution:

def filter_unique(records, columns):
    unique = set(tuple(rec[col] for col in columns) for rec in records)
    return [dict(zip(columns, items)) for items in unique]

It first generates a tuple of column values for each record, then removes non-unique occurrences with set(), then reconstructs dictionary by giving names to each value in a tuple.

Call it like this :

filtered_records = filter_unique(records, ['col1','col2','col3'])

Disclaimer: I am a python beginner myself, so my solution might not be the best or the most optimized one.

Upvotes: 1

Related Questions