Oct-03-2017, 02:58 PM
First, note that there is a capitalize method of strings:
Second, when you loop through the list of words at the end, items is a copy of the word, not the word in the list. So you either need to get at the word in the list, or build a new list. Enumerate could get you at the word in the list:
In terms of efficiency, you are going through the list twice: once to upper case, then once to lower case. I would go through the list once, starting with a second empty list. Check each word as you go as to whether it needs to be capitalized or not, and then append the word with the appropriate capitalization to the second list. Also, the in operator works faster with sets, so you might want to make exceptions a set.
>>> 'spam'.capitalize() 'Spam'So you don't need to break the word apart, uppercase the first letter, and put it back together.
Second, when you loop through the list of words at the end, items is a copy of the word, not the word in the list. So you either need to get at the word in the list, or build a new list. Enumerate could get you at the word in the list:
numbers = [8, 0, 1] for num_index, num in enumerate(numbers): if num % 2: numbers[num_index] = num * 3 + 1 else: numbers[num_index] = num // 2 print(numbers)Also note that 'in' can be used as an operator to check if something is in a list or other container. You can just use
if items in exceptions:
.In terms of efficiency, you are going through the list twice: once to upper case, then once to lower case. I would go through the list once, starting with a second empty list. Check each word as you go as to whether it needs to be capitalized or not, and then append the word with the appropriate capitalization to the second list. Also, the in operator works faster with sets, so you might want to make exceptions a set.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures