Sort strings by value and add to list

python sort list of strings with numbers
python list sort by length descending
how to sort a list alphabetically in python without sort function
python sort list with letters and numbers
sort dictionary by value python
python sort list of lists
python sort list of strings by length
how to sort numbers in python without sort function

I have this list of log files that I want to sort by the date inside each one: as you can see, there is after LOG_ a number which is the key I want to sort the string.

The date is in yyyymmdd format.

LOGS\LOG_20190218_91_02.LOG
LOGS\LOG_20190218_91_05.LOG
LOGS\LOG_20190218_91_00.LOG
LOGS\LOG_20190218_91_22.LOG
LOGS\LOG_20190218_91_10.LOG
LOGS\LOG_20190219_56_22.LOG
LOGS\LOG_20190219_56_24.LOG
LOGS\LOG_20190219_56_25.LOG
LOGS\LOG_20190219_56_26.LOG
LOGS\LOG_20190219_56_03.LOG
LOGS\LOG_20190220_56_22.LOG
LOGS\LOG_20190220_56_07.LOG
LOGS\LOG_20190220_56_13.LOG
LOGS\LOG_20190220_56_17.LOG
LOGS\LOG_20190220_56_21.LOG

I tried various approaches:

  1. extract the date value, add them to list, distinct them (using set) and, by each one, take the string/filepath and add it to a list. The problem is that dates could vary in size (here there are only 3, but they could be more). So using fixed lists is (maybe) out of scope.

  2. verify each string and check with the previous/next to see if the date changed. If changed, then add all the previous paths/string to a list. Still same problem but maybe this approach could be improved.

  3. manually copy-paste the files in folders for each date and then work with them. This is out of scope by now because we are talking about huge files (gigs).

What I would like to understand is how the second soulution could be implemented. How can properly store the files/strings with same date in a own list ?

Expected result...

list20190218 = [all LOG files with 20190218 value in name]
list20190219 = [all LOG files with 20190219 value in name]
list20190220 = [all LOG files with 20190220 value in name]

...but with a variable number of lists.

Thanks

A clean way to do this would be using dictionaries. In this case the keys would be the dates and the values would be the corresponding list. In order to group the elements in the list you could use itertools.groupby. You also need to specify that you want to group the list using the date, for that you can extract the date substring from each string in the key argument:

from itertools import groupby
from operator import itemgetter

d = {k:list(v) for k,v in groupby(data, key=lambda x: itemgetter(1)(x.split('_')))}

Then simply do:

d['20190220']

['LOGS\\LOG_20190220_56_22.LOG\n',
 'LOGS\\LOG_20190220_56_07.LOG\n',
 'LOGS\\LOG_20190220_56_13.LOG\n',
 'LOGS\\LOG_20190220_56_17.LOG\n',
 'LOGS\\LOG_20190220_56_21.LOG']

Python, Sorting the integer values in list of string etc. Let's discuss various ways to perform this task. Example #1: Using sort() function. To Sort a list of strings by length, provide len() as key function in sort i.e. ''' Sort List of string by Length by using len() as custom key function ''' listOfStrings.sort(key=len) Now list contents will be,

Code below.

Create a named tuple that will keep the file date. Sort the list using the date as key.

from collections import namedtuple, defaultdict
import datetime

FileAttr = namedtuple('FileAttr', 'name date')
files = ['LOGS\LOG_20190218_91_02.LOG',
         'LOGS\LOG_20190218_91_05.LOG',
         'LOGS\LOG_20190218_91_00.LOG',
         'LOGS\LOG_20190218_91_22.LOG',
         'LOGS\LOG_20190218_91_10.LOG',
         'LOGS\LOG_20190219_56_22.LOG',
         'LOGS\LOG_20190219_56_24.LOG',
         'LOGS\LOG_20190219_56_25.LOG',
         'LOGS\LOG_20190219_56_26.LOG',
         'LOGS\LOG_20180219_56_26.LOG',
         'LOGS\LOG_20170219_56_26.LOG',
         'LOGS\LOG_20190219_56_03.LOG',
         'LOGS\LOG_20190220_56_22.LOG',
         'LOGS\LOG_20190220_56_07.LOG',
         'LOGS\LOG_20190220_56_13.LOG',
         'LOGS\LOG_20190220_56_17.LOG',
         'LOGS\LOG_20190220_56_21.LOG']
files_ex = []

for f in files:
    left_idx = f.find('_')
    right_idx = f.find('.')
    date_part = f[left_idx + 1:right_idx][:-6]
    year = int(date_part[:4])
    month = int(date_part[4:6])
    day = int(date_part[6:8])
    dt = datetime.datetime(year, month, day)
    files_ex.append(FileAttr(f, dt))
sorted_files_ex = sorted(files_ex, key=lambda x: x[1])

files_by_date = defaultdict(list)
for file_attr in sorted_files_ex:
    files_by_date[file_attr.date].append(file_attr.name)
for date, files in files_by_date.items():
    print('{} --> {}'.format(date, files))

Output:

2019-02-18 00:00:00 --> ['LOGS\\LOG_20190218_91_02.LOG', 'LOGS\\LOG_20190218_91_05.LOG', 'LOGS\\LOG_20190218_91_00.LOG', 'LOGS\\LOG_20190218_91_22.LOG', 'LOGS\\LOG_20190218_91_10.LOG']
2019-02-19 00:00:00 --> ['LOGS\\LOG_20190219_56_22.LOG', 'LOGS\\LOG_20190219_56_24.LOG', 'LOGS\\LOG_20190219_56_25.LOG', 'LOGS\\LOG_20190219_56_26.LOG', 'LOGS\\LOG_20190219_56_03.LOG']
2017-02-19 00:00:00 --> ['LOGS\\LOG_20170219_56_26.LOG']
2018-02-19 00:00:00 --> ['LOGS\\LOG_20180219_56_26.LOG']
2019-02-20 00:00:00 --> ['LOGS\\LOG_20190220_56_22.LOG', 'LOGS\\LOG_20190220_56_07.LOG', 'LOGS\\LOG_20190220_56_13.LOG', 'LOGS\\LOG_20190220_56_17.LOG', 'LOGS\\LOG_20190220_56_21.LOG']

Sort numeric strings in a list in Python, Sorting list is an easy task and has been dealt with in many situations. sometimes we can get the data in the format of list of numbers but with string as data type. list of strings in case-insensitive manner � Python | Add only numeric values� List<T>.Sort() Method is used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements. There are total 4 methods in the overload list of this method as follows:

I'll post also my solution. It's more verbose but maybe a little bit easier to be understood than list comprehension.

import os
import glob
from itertools import groupby
from operator import itemgetter

LOGS = ['LOGS\LOG_20190218_91_02.LOG',
        'LOGS\LOG_20190218_91_05.LOG',
        'LOGS\LOG_20190218_91_00.LOG',
        'LOGS\LOG_20190218_91_22.LOG',
        'LOGS\LOG_20190218_91_10.LOG',
        'LOGS\LOG_20190219_56_22.LOG',
        'LOGS\LOG_20190219_56_24.LOG',
        'LOGS\LOG_20190219_56_25.LOG',
        'LOGS\LOG_20190219_56_26.LOG',
        'LOGS\LOG_20190219_56_03.LOG',
        'LOGS\LOG_20190220_56_22.LOG',
        'LOGS\LOG_20190220_56_07.LOG',
        'LOGS\LOG_20190220_56_13.LOG',
        'LOGS\LOG_20190220_56_17.LOG',
        'LOGS\LOG_20190220_56_21.LOG']

dateList = []

for log in LOGS:
    baseName = os.path.basename(log)
    date = baseName.split('_')[1][:8]
    dateList .append(date)

dateList = (set(dateList))

myDict = {}

for date in dateList:
    for log in LOGS:
        if date in log:
            myDict.setdefault(date, [])

            myDict[date].append(log)

for key, value in myDict.items():
    print(key, value)

Output:

20190220 ['LOGS\\LOG_20190220_56_22.LOG', 'LOGS\\LOG_20190220_56_07.LOG', 'LOGS\\LOG_20190220_56_13.LOG', 'LOGS\\LOG_20190220_56_17.LOG', 'LOGS\\LOG_20190220_56_21.LOG']
20190219 ['LOGS\\LOG_20190219_56_22.LOG', 'LOGS\\LOG_20190219_56_24.LOG', 'LOGS\\LOG_20190219_56_25.LOG', 'LOGS\\LOG_20190219_56_26.LOG', 'LOGS\\LOG_20190219_56_03.LOG']
20190218 ['LOGS\\LOG_20190218_91_02.LOG', 'LOGS\\LOG_20190218_91_05.LOG', 'LOGS\\LOG_20190218_91_00.LOG', 'LOGS\\LOG_20190218_91_22.LOG', 'LOGS\\LOG_20190218_91_10.LOG']

If you use print(myDict["20190220"])...

['LOGS\\LOG_20190220_56_22.LOG', 'LOGS\\LOG_20190220_56_07.LOG', 'LOGS\\LOG_20190220_56_13.LOG', 'LOGS\\LOG_20190220_56_17.LOG', 'LOGS\\LOG_20190220_56_21.LOG']

How to Use sorted() and sort() in Python – Real Python, .split() can change this behavior and clean up the output, and .join() can put it all sorted() can be used on a list of strings to sort the values in ascending order,� 3. Click Finish, and the text strings have been split into columns, and select the column data which you want to sort strings by, click Data > Sort A to Z, and check Expand the selection option in the Sort Warning dialog. See screenshot: 4. Click Sort. Now the text strings have been sorted by middle characters, you can remove the helper columns.

Python Sorting | Python Education, Custom Sorting With key= sort() method; Tuples; List Comprehensions The sort calls len() for each string to get the list of proxy length values, and then You can add an if test to the right of the for-loop to narrow the result. Sort(); foreach (string value in list) { Console.WriteLine(value); } } } Output angler tuna velvetfish Example, LINQ. Here we use the LINQ orderby keyword to sort a List by any property.

Python : How to Sort a list of strings ?, By default its value is False, but if its set to True then it will sort the list in reverse order. So, contents of list will be now,. Return value from sort() The sort() method doesn't return any value. Rather, it changes the original list. If you want a function to return the sorted list rather than change the original list, use sorted().

Python: How to Sort a List? (The Right Way), So how can you sort a list of strings in a sort and sorted added an optional� The sort is case-sensitive. For more information on sorting character and string arrays, see Sort Order for Character and String Arrays. If A is a string array, then sort reorders the elements of the array, but does not reorder characters within the strings.

Comments
  • what is this, a txt file? A list?
  • Sorry, it is a list of paths.
  • Could you post some code? For me it's hard to grasp what you're actually doing in each approach; it seems you could simply use list.sort(), but probably I missed something...
  • I don't want only to sort the strings (I already did it in fact) but to create, in this case, a container (list, dict) which will contain all the string with the same date. A problem is that the number of containers could vary.
  • Can you please provvide also a non-comprehension list format of the above code ?