1

I have a list that contains elements:

['16:11:40.894 0,Type=IsXover,Count=1,lp-isD=2'
 '16:11:40.894 0,Type=IsXover,Count=54,lp-xsD=1'
 '16:11:40.894 0,Type=IsXover,Count=201,lr-isD=3'
 '16:11:40.894 0,Type=IsXover,Count=3075,lp-gsD=5']

I want to sort this list based on the number after 'Count='. I can't do it with .sort(key=lambda x: x[37]) as it has been said here because my digits become double, triple, ... digits. How can I sort this list without using regex?

(Please not the list is pretty long, I wrote a summarised version of the list above)

2
  • You are not allowed to use regex? That makes it much harder. Commented May 2, 2018 at 15:45
  • 1
    sorted(lst, key=lambda x: int(x.split('Count=', 1)[1].split(',', 1)[0])) Commented May 2, 2018 at 15:51

5 Answers 5

2

This does it:

to_sort = ['16:11:40.894 0,Type=IsXover,Count=201,lr-isD=3',
           '16:11:40.894 0,Type=IsXover,Count=54,lp-xsD=1',
           '16:11:40.894 0,Type=IsXover,Count=1,lp-isD=2',
           '16:11:40.894 0,Type=IsXover,Count=3075,lp-gsD=5']
def key(x:str):
    return int(x.partition("Count=")[2].partition(",")[0])

print(sorted(to_sort, key=key))
Sign up to request clarification or add additional context in comments.

Comments

0
lst = ['16:11:40.894 0,Type=IsXover,Count=1,lp-isD=2',
       '16:11:40.894 0,Type=IsXover,Count=54,lp-xsD=1',
       '16:11:40.894 0,Type=IsXover,Count=201,lr-isD=3',
       '16:11:40.894 0,Type=IsXover,Count=3075,lp-gsD=5']

def extract_num(text):
    txt1 = text[text.find("Count=")+6:]
    txt2 = txt1[:txt1.find(",")]
    return int(txt2)

lst.sort(key=extract_num)

print(lst)

Comments

0

Without regex, and assuming the same format for all your strings, you could do this:

mylist.sort(key = lambda x: int(''.join([i for i in x.split(',')[2] if i.isnumeric()])))

The list comprehension [i for i in x.split(',')[2] if i.isnumeric()] splits your string at the commas, takes the element at index 2 (which would be "Count=___"), and extracts all the numeric strings. Then, int(''.join joins those together and casts it to an integer. You can then use this as your key to sort your list.

Comments

0
lst = ['16:11:40.894 0,Type=IsXover,Count=1,lp-isD=2',
       '16:11:40.894 0,Type=IsXover,Count=54,lp-xsD=1',
       '16:11:40.894 0,Type=IsXover,Count=201,lr-isD=3',
       '16:11:40.894 0,Type=IsXover,Count=3075,lp-gsD=5']

count_dict = {}

for elem in lst:
    temp_list = elem.split(',')
    count = temp_list[2]

    new_count = int(count.split('=')[1])

    count_dict[new_count] = elem

new_list = []

for count in sorted(count_dict):
    new_list.append(count_dict[count])

Comments

0

You can try the method told by Chris_Rands.

Try to extract the value of the Count parameter from the string by splitting the whole string.

sorted(lst, key=lambda x: int(x.split('Count=', 1)[1].split(',', 1)[0]))

The above statement first splits the string based on the key 'Count='. So the part of the string before this can be neglected and thus we take the second part of the string by using index 1. in this part we again split the string on ','. and then neglect the part after it. So we take only the part before ',' by using index 0. And finally, we parse this value to integer type.

For ex. take '16:11:40.894 0,Type=IsXover,Count=1,lp-isD=2'

after splitting the string from 'Count=' and taking the value at index 0, we get '1,lp-isD=2'.

now splitting this from ',' and taking the value at index 0, we get '1'.

So after parsing this to function int(), we get the value of Count.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.