2

I'm at a place in my code where I need to make the order of the contents in one string equal the order of the other. So say you put the string 'this place' into my function you will get TIPAEhslc returned and I want to change the order of the contents of that to match the original input string without the spaces, so: 'ThIsPlAcE'. So, in my code, using no_space to order s_combine I know I've figure this out before but I can't remember how and I've been stuck on this ! This is my code so far:

def r_string(string):
    s_even = []
    s_odd = []
    s_compare = []
    s_split = [[x] for x in string.split()]
    for i in s_split:
        for u in i:
            s = enumerate(u)
            for j,k in s:
                if j % 2 == 0:
                    s_even.append(k)
                else:
                    s_odd.append(k)
    s_even = ''.join(s_even).upper()
    s_odd = ''.join(s_odd).lower()
    s_combine = s_even + s_odd
    no_space = string.replace(" ","")

1 Answer 1

2

Try this:

def r_string(s):
    n = list()
    for i, a in enumerate(s.replace(' ', '')):
        if i%2==0:
            n.append(a.upper())
        else:
            n.append(a)

    return ''.join(n)

 print(r_string('this place'))      

Output:

#ThIsPlAcE

Edit:

I guess you r looking for this....

def r_string(s1, s2):
    #s1 is string to order
    #s2 string map to follow
    map_ = {s.lower():s.istitle() for s in s2}
    new_string = ''.join([s.upper() if map_[s] else s for s in s1.replace(' ', '')])

    return new_string

print(r_string('string me', 'TrinGsMe'))

Output:

#sTrinGMe
Sign up to request clarification or add additional context in comments.

2 Comments

so this is great, it works for this stage of my code and makes most of my code look blocky and unwarranted but I still need the functionality of ordering a string by another string.
so if I had 'hOij' and I want this to be in the order of 'ohij' but like 'Ohij' since the o is captial in 'hOij'. it needs to retain the same character case (upper or lower) while taking the ordering of the ordering string

Your Answer

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