I am newbie in Python and I need help:
I have a file with lines like these(see below). I want to copy lines from this file and since some lines have similar date, If lines have similar date I want to choose line with smallest value of the number ending the list.
'1990-01-01','f','2'
'1990-01-02','c','4'
'1990-01-01','j','2.5'
'1990-01-01','j','1.5'
'1990-01-02','b','3.9'
Some code first:
I’ve put the text in the code and wrapped it in a StringIO object to emulate a file, but normally one would open the text using the csv.reader on a file using open().
Then the data is sorted by the data column (index 0), and the value column (index 2) – this sort order ensures that identical dates are together (I realise you mentioned ‘similar’, but this recipe can be adapted) and that for each date, the lowest value column appears first.
itertools.groupby() is used to iterate over keys that match certain criteria – where they meet that criteria, they are in the same “group”. ‘key’ (what is being grouped on) will be the value of the date, and ‘val’ will be another iterable of available values within that key. Since only the first is required, using next(val) will return the result with the smallest value.
The output is: