1

I am trying to solve this sorting problem of a list of strings.

All strings begin with an alphanumeric identifier and after the alphanumeric identifier the string will consist of either words or integers.

The output list needs to be organised following these rules:

  • the strings with words must be at the beginning of the list
  • The strings with words are ordered lexicographically, ignoring the identifier
  • the strings with integers must be left in the original order
  • the identifiers must be part of the output strings

example

list = ['a1 9 2 3 1', 'g1 act car', 'zo4 4 7', 'ab1 off key dog', 'a8 act zoo']

reordered_list = [ 'g1 act car', 'a8 act zoo', 'ab1 off key dog', 'a1 9 2 3 1', 'zo4 4 7']

I'm trying to split the strings use the lambda function to sort by the 2nd value but the output is None

list.sort(key = lambda x: x.split()[1])

martineau
  • 119,623
  • 25
  • 170
  • 301
kirtap
  • 121
  • 1
  • 5
  • `.sort` sorts inplace and returns `None`. You might want `sorted()`. – gmds Feb 19 '19 at 23:37
  • 1
    "the strings with words must be at the beginning of the list". What is a "word"? Nothing in your code attempts to determine whether the string has any linguistic meaning, nor is that an easy task to accomplish. Why is "abc" not a word, in the eyes of a computer? – roganjosh Feb 19 '19 at 23:39

1 Answers1

0

You can use the sorted function with a key function that returns a tuple of 1 or 2 items based on whether the last character is a digit (since presumably it represents whether it's a string with integers), and prioritize strings with words over strings with integers by giving the first item of the tuple for a string with words a smaller number:

sorted(l, key=lambda s: (1,) if s[-1].isdigit() else (0, s.split(' ', 1)[1]))

This returns:

['g1 act car', 'a8 act zoo', 'ab1 off key dog', 'a1 9 2 3 1', 'zo4 4 7']
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • Thanks @blhsing! Im now trying to sort the elements in the new list based on the first element of the string only if the other elements are common to another string. I wrote this code to 'swap' the elements `for num, elem in enumerate(sorted_list[:-1]): if elem.split(' ',1)[1:] == sorted_list[num+1].split(' ',1)[1:] and \ elem.split(' ',1)[0] > sorted_list[num+1].split(' ',1)[0]: elem[num], elem[num+1] = elem[num+1], elem[num] ` But I keep getting this error: 'str' object does not support item assignment – kirtap Feb 20 '19 at 19:56
  • Glad to be of help. Not to sure what you mean by "only if the other elements are common to another string". Since this is fundamentally a different problem, please post this as a new question with sample input and expected output so that it would be clear what you mean. – blhsing Feb 20 '19 at 19:59