Merge list concating unique values as comma seperated retaining original order from csv

2024/10/13 1:22:28

Here is my data:

data.csv

id,fname,lname,education,gradyear,attributes
1,john,smith,mit,2003,qa
1,john,smith,harvard,207,admin
1,john,smith,ft,212,master
2,john,doe,htw,2000,dev

Here is the code:

from itertools import groupby
import csv
import pprintt = csv.reader(open('data.csv'))
t = list(t)def join_rows(rows):return [(e[0] if i < 3 else ','.join(e)) for (i, e) in enumerate(zip(*rows))]for name, rows in groupby(sorted(t), lambda x:x[0]):print join_rows(rows)

It works, but while merging it doest not retain original order, instead it works from concating last to first

Output is:

['1', 'john', 'smith', 'ft,harvard,mit', '212,207,2003', 'master,admin,qa']
['2', 'john', 'doe', 'htw', '2000', 'dev']
['id', 'fname', 'lname', 'education', 'gradyear', 'attributes']

Instead of:

['1', 'john', 'smith', 'mit,harvard,ft', '2003,207,212', 'qa,admin,master']
['2', 'john', 'doe', 'htw', '2000', 'dev']
['id', 'fname', 'lname', 'education', 'gradyear', 'attributes']

as it is listed in the CSV file (original order)

My approach to fix this would be to rerun though values and try to reverse it. Is there any cleverer approach?

Answer

The problem is sorting, which is not required. Change as:

groupby(t, lambda x:x[0])
https://en.xdnf.cn/q/118138.html

Related Q&A

Unable to submit Spark job from Windows IDE to Linux cluster

I just read about findspark and found it quite interesting, as so far I have only used spark-submit which isnt be suited for interactive development on an IDE. I tried executing this file on Windows 10…

Updating variable values when running a thread using QThread in PyQt4

So problem occurred when I tried using Threading in my code. What I want to do is passing default values to the def __init__ and then calling the thread with its instance with updated values but someho…

Show terminal status in a Tkinter widget

I am using python2.7.10 on MacOs Sierra and have created a rsync over ssh connection with my raspberrypi. The Idea is to synchronize my local folder with my remote folder on the raspberrypi. My functio…

Python Convert HTML into JSON using Soup

These are the rulesThe HTML tags will start with any of the following <p>, <ol> or <ul> The content of the HTML when any of step 1 tags is found will contain only the following tags: …

I/O Error while saving Excel file - Python

Im using python to open an existing excel file and do some formatting and save and close the file. My code is working good when the file size is small but when excel size is big (apprx. 40MB) Im gettin…

How to stop the python turtle from drawing

Can anyone tell me why this code always has a line on the screen and also how to stop it?Slight problem with this is that every time this happens, I always get a line on my canvas no matter what I try…

Replace values in a string

So the challenge was to replace a specific word in a sentence with asterisks with equivalent length to that word - 3 letters 3 asterisks etc.Section One does not work, but Section Two does - can anyon…

Select n data points from plot

I want to select points by clicking om them in a plot and store the point in an array. I want to stop selecting points after n selections, by for example pressing a key. How can I do this? This is wha…

Python azure uploaded file content type changed to application/octet-stream

I am using python Azure sdk. When the file uploaded its content type changed to application/octet-stream. I want to set its default content type like image/png for PNG image.I am using following method…

the dumps method of itsdangerous throws a TypeError

I am following the guide of 『Flask Web Development』. I want to use itsdangerous to generate a token, but some problems occured. Here is my code:def generate_confirmation_token(self, expiration=3600):…