Reorder String based on Specified Character in Python -


i'm looking way in python change, example, string "main st & 1st st" string "1st st & main st".

i know effect of:

intersection = "main st & 1st st" intersectionlist = intersection.split(" & ") reversedintersection = (" & ".join(intersectionlist[::-1])).strip() 

but seems needlessly multi-stepped , wondering if there more efficient method, built-in method, etc... accomplish same goal better.

i don't think way multi-stepped. shortest way can find describe want is:

  1. take string

  2. split @ ampersands

  3. reverse substrings

  4. write down substrings, separating them ampersands

that's code does. if want code more compact, can leave .strip() @ end since isn't useful , rewrite 2nd , 3rd lines this:

reversedintersection = (" & ".join(intersection.split(" & ")[::-1]))

i don't know of built-in method this, since can code same work takes describe , isn't common problem.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -