0

I have a string as ('3% Spandex,60% Polyester,7% Cotton,30% Other') and i want to extract the highest value which in this case would be 60% Polyester So I think it will work if I split the string into a list and then strip all numeric values which should enable me to find the position of the max value and find the highest value using that. But this is a lenghty process which I guess would slow me down. Is there any other way to do it?

sahil
  • 15
  • 4
  • Do you only care for the numeric part? – David Oct 20 '20 at 07:37
  • @DavidS No the end goal is to find the highest composition which here would be 60% polyester – sahil Oct 20 '20 at 07:39
  • Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Oct 20 '20 at 07:39
  • You have to go through them somehow, but you could possibly skip the list part by using string operations. ie. regex – Flashcap Oct 20 '20 at 07:40
  • 1
    `sorted('3% Spandex,60% Polyester,7% Cotton,30% Other'.split(","),key=lambda x: int(x.split("%")[0]),reverse=True)[0]`?? Could not post answer because it was closed. – Wasif Oct 20 '20 at 07:40

1 Answers1

1

The following should work:

res=' '.join(max([i.split() for i in s.split(',')], key=lambda x:int(x[0].split('%')[0])))

>>> print(res)
'60% Polyester'
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30