How to perform string,upper() for every string in one list based on the indexes from another list in Python?

Viewed 34

I've been trying to make a Python script recently that replaces all words in an inputted string with randomized words from a list, and I've been wanting it to also mirror the capitalization of the original sentence, so I tried to accomplish this by getting the indexes of all capitalized words from one string and then using string.upper() on the indexes in the second string. However, I can't figure out how to perform the string,upper() operation on the other list through the list of indexes I already have, so fixing this or suggesting a better way of doing this would greatly help! Here's a summary of my other code here

import random

#wordlist
wordlist = ['Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit', 'suspendisse', 'diam', 'congue', 'molestie', 'nec', 'aliquam']

#input sentence
sentence = str(input('\nEnter the sentence here: '))

#word amount
wordamount = len(sentence.split())

#insert random array
sentence2 = (random.choices(wordlist, k=wordamount))

#check and reinsert uppercase letters
capitalized = []
for idx, x in enumerate(sentence.split()):
    if x == x.capitalize() and (len(x)!= len(x) < 1) != x.upper():
        capitalized.append(idx)

#print new sentence
#os.system('clear')
print('New Sentence: ' + ' '.join(sentence2))

0 Answers
Related