I have a list of strings representing numbers. I can't use int because some of the numbers have attached letters, like '33a' or '33b'
['21', '23a', '23b', '23k', '23l', '23x', '25', '33a', '33b', '33c', '33d', '33e', '33f', '34', '34', '35a', '35a' ]
My goal is to concatenate the numbers to one string and separate them using a forward slash.
If a number is repeated and its additional letters continue in alphabetical order, the representation should be simplified as follows:
['23a'/'23b'] --> '23a-b'
If a number is repeated without additional letters, it should be listed only once. The same applies to repeating identical pairs of numbers and additional letters.
For the complete example, the desired output looks like this:
'21/23a-b/23k-l/23x/25/33a-f/34/35a'
Using the following code I am able to concatenate the numbers and exclude duplicates, but I fail in trying to simplify the numbers with letters according to the above example.
numbers = ['21', '23a', '23b', '23k', '23l', '23x', '25', '33a', '33b', '33c', '33d', '33e', '33f', '34', '34', '35a', '35a' ]
concat_numbers = ""
numbers_set = list(set(numbers))
numbers_set.sort()
for number in numbers_set:
concat_numbers += number + "/"
print(concat_numbers)
>>> '21/23a/23b/23k/23l/23x/25/33a/33b/33c/33d/33e/33f/34/35a/'
Any hints on how to achieve this in the most pythonic way?