1
archive= ['Frederick the Great',
          'Ivan the Terrible',
          'maurice Of Nassau',
          'Napoleon BONAPARTE']

   n=[]
   n2=[]
   n3[]

    for line in archive:
        n.append(line)

    for x in n :
        lw = x.lower()
        for i in lw.split() :
            n2.append(i)

    for i in n2 :

        if i == 'of' or i == 'the' :
            i=i.lower()
            n3.append(i)

        else: 
            i=i.capitalize()
            n3.append(i) 

    print(n3)

this code prints the names as strings, how could .join() be used to do that or using some other method making so that the output would be words in the names are capitalized, the and of are in lowercase and together. PS:Still new to programming sorry for any mistakes in the formulation of the question.

6
  • 1
    What is your desired output? And what's actually in archive? right now it's probably a syntax error Commented Mar 13, 2017 at 20:56
  • Your archive section doesn't have quotes or commas. Make sure your code runs before posting an example. Commented Mar 13, 2017 at 20:59
  • n, n2 and n3 will cause errors because they are not initialized. For example, n = [] Commented Mar 13, 2017 at 21:04
  • i thinnk i fixed the syntax errors now Commented Mar 13, 2017 at 21:06
  • the out put should be something like 'Frederick the Great', 'Ivan the Terrible','Maurice of Nassau','Napoleon Bonaparte' However i'm getting 'Ivan','the',Terrible','Maurice','of','Nassau' Commented Mar 13, 2017 at 21:09

3 Answers 3

1

Expecting there are no quotations or punctuations, you can do as follows

archive = ['Frederick the Great',
          'Ivan the Terrible',
          'maurice Of Nassau',
          'Napoleon BONAPARTE']

reformated = [
    ' '.join(word.capitalize()
             if word not in ('the', 'of')
             else word
             for word in line.lower().split())
    for line in archive
]

['Frederick the Great',
 'Ivan the Terrible',
 'Maurice of Nassau',
 'Napoleon Bonaparte']
Sign up to request clarification or add additional context in comments.

Comments

0

I write the short function to do capitalize the given string. You can use map in order to apply to all list of string in the list.

archive= ['Frederick the Great',
          'Ivan the Terrible',
          'maurice Of Nassau',
          'Napoleon BONAPARTE']

def capitalize_phrase(text):
    s_capitalize = []
    for s in text.split(' '):
        if s.lower() in ['the', 'of']:
            s_capitalize.append(s.lower())
        else:
            s_capitalize.append(s.capitalize())
    return ' '.join(s_capitalize)

print(list(map(capitalize_phrase, archive)))

Comments

0

Another solution:

archive= [
    'Frederick the Great',
    'Ivan the Terrible',
    'maurice Of Nassau',
    'Napoleon BONAPARTE'
]

lines = []
for line in archive:
    line = line.lower().split()
    for i, word in enumerate(line):
        if word in ('of', 'the'):
            continue
        line[i] = word.capitalize()
    lines.append(' '.join(line))

print lines

The advantage of this solution is that it lowers the line in 1 shot, and just continues to the next word when the word if 'of' or 'the', which saves processing cycles.

Comments

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.