2

I have the code below. Basically, I want to take an array like ["appleBanana", "bananaFruit", "cherryBlossom"] and turn it into ["Apple Banana", "Banana Fruit", "Cherry Blossom"].

This is in Python.

Reproducible example:

typelist = ["appleBanana", "bananaFruit", "cherryBlossom"]
  

typelist2 = ['']
last_was_upper = True

counter = 0

for d in typelist:
    for c in d:
        if c.isupper():
            if not last_was_upper:
                typelist2[counter] += ' '
            last_was_upper = True
        else:
          last_was_upper = False
          
        typelist2[counter] += c 
        
    counter = counter + 1
      
  
 


print(typelist2)




Edit

For my actual code, this is the output I got

[' Advanced Algebra', ' Problem Solvingand Data Analysis', ' Basic Algebra', ' Problem Solvingand Data Analysis', ' Advanced Algebra', ' Advanced Algebra', ' Advanced Algebra', ' Problem Solvingand Data Analysis', ' Advanced Algebra', ' Problem Solvingand Data Analysis', ' Problem Solvingand Data Analysis', ' Problem Solvingand Data Analysis', ' Advanced Algebra', ' Problem Solvingand Data Analysis', ' Advanced Algebra', ' Advanced Algebra', ' Advanced Algebra', ' Advanced Algebra', ' Advanced Algebra', ' Problem Solvingand Data Analysis']

Notice how some of them have a space before the phrase while others have no space between the letters. My desired outcome is simply "Advanced Algebra" (with respective fields obviously). How can I remove the space in the beginning and add a space in the middle for all of them?

2
  • Can you post part of your actual data? Commented Jun 28, 2020 at 21:50
  • @AnnZen ['AdvancedAlgebra', 'ProblemSolvingandDataAnalysis', 'BasicAlgebra', 'ProblemSolvingandDataAnalysis', 'AdvancedAlgebra', 'AdvancedAlgebra', 'AdvancedAlgebra', 'ProblemSolvingandDataAnalysis', 'AdvancedAlgebra', 'ProblemSolvingandDataAnalysis', 'ProblemSolvingandDataAnalysis', 'ProblemSolvingandDataAnalysis', 'AdvancedAlgebra', 'ProblemSolvingandDataAnalysis', 'AdvancedAlgebra', 'AdvancedAlgebra', 'AdvancedAlgebra', 'AdvancedAlgebra', 'AdvancedAlgebra', 'ProblemSolvingandDataAnalysis'] Commented Jun 28, 2020 at 22:21

4 Answers 4

1

You can achieve your output using a simple list comprehension in a one line solution:

import re
typelist=["appleBanana", "bananaFruit", "cherryBlossom"]
typelist2=[re.sub(r"(\w)([A-Z])", r"\1 \2", element).title() for element in typelist]

Output:

>>> typelist2
['Apple Banana', 'Banana Fruit', 'Cherry Blossom']

The re module is included within the standard python distribution and is needed to insert a whitespace before capital letters.

The title() method is called to capitalize the first letter.

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

Comments

1

Here's a solution without regex (nothing wrong with it, just a different approach):

typelist = ["appleBanana", "bananaFruit", "cherryBlossom"]

# for word in typelist
#   for letter in word:
#       if letter is uppercase, split word by index of letter,
#       then uppercase the first letter and put a space between the two words.

fixed_typelist = []
for word in typelist:
    for index, letter in enumerate(word):
        if letter.isupper():
            split_letter = word[:index].title(), word[index:]
            fixed_word = ' '.join(split_letter)
            fixed_typelist.append(fixed_word)

print(fixed_typelist)
>>> ['Apple Banana', 'Banana Fruit', 'Cherry Blossom']

Essentially, the core problem is identifying the index (or position) of the letter that is capitalized. I choose to do that through enumerate, which returns an iterable of both the index of the letter and the letter itself. Once you've found that index, you just split the word apart and capitalize the first letter of the first word.

Comments

1

Another version without regex:

typelist = ["appleBanana", "bananaFruit", "cherryBlossom"]

for s in typelist:
    c, c2 = [], []
    for ch in s:
        if ch.isupper():
            c, c2 = c2, c
        c.append(ch)
    print(''.join(c2).title(), ''.join(c).title())

Prints:

Apple Banana
Banana Fruit
Cherry Blossom

EDIT:

typelist = ["appleBanana", "bananaFruit", "cherryBlossom", 'problemSolvingAndDataAnalysis']

out = []
for s in typelist:
    words = [[]]
    for ch in s:
        if ch.isupper():
            words.append([])
        words[-1].append(ch)
    out.append(' '.join(''.join(w).title() for w in words))

print(out)

Prints:

['Apple Banana', 'Banana Fruit', 'Cherry Blossom', 'Problem Solving And Data Analysis']

3 Comments

@user13824876 What is your expected output for ProblemSolvingandDataAnalysis ?
Problem Solving and Data Analysis
The 'And' should be 'and'.
1

Given you actual list, you can do:

import re
lst = ['AdvancedAlgebra', 'ProblemSolvingandDataAnalysis', 'BasicAlgebra', 'ProblemSolvingandDataAnalysis', 'AdvancedAlgebra', 'AdvancedAlgebra', 'AdvancedAlgebra', 'ProblemSolvingandDataAnalysis', 'AdvancedAlgebra', 'ProblemSolvingandDataAnalysis', 'ProblemSolvingandDataAnalysis', 'ProblemSolvingandDataAnalysis', 'AdvancedAlgebra', 'ProblemSolvingandDataAnalysis', 'AdvancedAlgebra', 'AdvancedAlgebra', 'AdvancedAlgebra', 'AdvancedAlgebra', 'AdvancedAlgebra', 'ProblemSolvingandDataAnalysis']
lst = [' '.join(re.findall(r'[A-Z][a-z]*',s)) for s in lst]
print(lst)

Note that this will treat the lower case 'and's as part of the word before it.

If you're willing to risk words like 'Command' to turn into Comm and, you can do:

import re
lst = ['AdvancedAlgebra', 'ProblemSolvingandDataAnalysis', 'BasicAlgebra', 'ProblemSolvingandDataAnalysis', 'AdvancedAlgebra', 'AdvancedAlgebra', 'AdvancedAlgebra', 'ProblemSolvingandDataAnalysis', 'AdvancedAlgebra', 'ProblemSolvingandDataAnalysis', 'ProblemSolvingandDataAnalysis', 'ProblemSolvingandDataAnalysis', 'AdvancedAlgebra', 'ProblemSolvingandDataAnalysis', 'AdvancedAlgebra', 'AdvancedAlgebra', 'AdvancedAlgebra', 'AdvancedAlgebra', 'AdvancedAlgebra', 'ProblemSolvingandDataAnalysis']
lst = [' '.join([s[:-3]+' and' if s.endswith('and') else s for s in re.findall(r'[A-Z][a-z]*',s)]) for s in lst]
print(lst)

Output:

['Advanced Algebra', 'Problem Solving and Data Analysis', 'Basic Algebra', 'Problem Solving and Data Analysis', 'Advanced Algebra', 'Advanced Algebra', 'Advanced Algebra', 'Problem Solving and Data Analysis', 'Advanced Algebra', 'Problem Solving and Data Analysis', 'Problem Solving and Data Analysis', 'Problem Solving and Data Analysis', 'Advanced Algebra', 'Problem Solving and Data Analysis', 'Advanced Algebra', 'Advanced Algebra', 'Advanced Algebra', 'Advanced Algebra', 'Advanced Algebra', 'Problem Solving and Data Analysis']

Comments

Your Answer

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