0

I was told to solve this but I'm not having an optimum solution

Lets say I have one string. This string is something like this

string= 'House 1 - New & Painted
         House 6
         House 2 - Used 
         House 4'

Now, i have to build a function that order this string taking account the house number, so the new string has to be something like this

string= 'House 1 - New & Painted
         House 2 - Used 
         House 4 
         House 6'

How can I do this in a function?

1 Answer 1

2

You can use .splitlines() to get list of lines and use str.split to find and convert the number to integer in key function:

s = """\
House 1 - New & Painted
House 6
House 2 - Used 
House 4"""

s = "\n".join(sorted(s.splitlines(), key=lambda v: int(v.split()[1])))
print(s)

Prints:

House 1 - New & Painted
House 2 - Used 
House 4
House 6
Sign up to request clarification or add additional context in comments.

1 Comment

Excelent solution :O

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.