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:
take string
split @ ampersands
reverse substrings
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
Post a Comment