Python for loop: index out of range error - python

I'm trying to create a loop that will take what the user entered and print it back alternating between upper and lowercase. Ex: the user enters 'helloworld', it prints back 'HeLlOwOrLd'. Here's what I have so far (trust me, I know it's not optimal, it's just what I could get to work):
s = input("enter characters: ")
word = ''
count = 0
for i in s:
up = s[::2]
up2 = up.upper()
up3 = up2[0 + count]
low = s[1::2]
low2 = low.lower()
low3 = low2[0 + count]
word += up3 + low3
count += 1
print(word)
When I trace it in the debugger, word comes to the right value, and then it runs the loop again, thus getting the index out of range error. Ideas?

Your looping through too many times. Your splitting s in half in the loop, but looping once for each character in s.
s = input("enter characters: ")
word = ''
for count in range(len(s[::2])):
up = s[::2]
up2 = up.upper()
up3 = up2[0 + count]
low = s[1::2]
low2 = low.lower()
if(len(low2)>count):
low3 = low2[0 + count]
else:
low3=''
word += up3 + low3
count += 1
print(word)
Cheers!

count will end up equal to the length of s. However, you are using count as an index for strings that are shorter than s, such as up3.
Try the following:
>>> result = ''
>>> word = 'hello'
>>> for i in range(len(word)):
... result += (word[i].lower() if i%2 else word[i].upper())
...
>>> result
'HeLlO'
Also:
>>> word = 'HellOWorLD'
>>> ''.join(word[i].lower() if i%2 else word[i].upper() for i in range(len(word)))
'HeLlOwOrLd'

You could use the case conversion functions from string and itertools.cycle to vary which one is applied to each character. Wrap that all up in a join statement and you get:
''.join(case(c) for case,c in
zip(itertools.cycle((string.upper,string.lower)), 'helloworld'))

Related

Is there any way to split string into array by spaces in another string in Python?

I have two input strings. In the first one - words with spaces. In the second - the word with same count of symbols without spaces. The task is to split the second string into array by spaces in the first one.
I tried to make it with cycles but there is problem of index out of range and i can't find another solution.
a = str(input())
b = str(input())
b_word = str()
b_array = list()
for i in range(len(a)):
if a[i] != " ":
b_word += b[i]
else:
b_array += b_word
b_word = str()
print(b_array)
Input:
>>head eat
>>aaabbbb
Output:
Traceback (most recent call last):
File "main.py", line 29, in <module>
b_word += b[i]
IndexError: string index out of range
Expected output:
>> ["aaab", "bbb"]
Thanks in advance!
Consider a solution based on iterator and itertools.islice method:
import itertools
def split_by_space(s1, s2):
chunks = s1.split()
it = iter(s2) # second string as iterator
return [''.join(itertools.islice(it, len(c))) for c in chunks]
print(split_by_space('head eat or', 'aaaabbbcc')) # ['aaaa', 'bbb', 'cc']
a = input() # you don't need to wrap these in str() since in python3 input always returns a string
b = input()
output = list()
for i in a.split(' '): # split input a by spaces
output.append(b[:len(i)]) # split input b
b = b[len(i):] # update b
print(output)
Output:
['aaab', 'bbb']
You can do something like this:
a = input()
b = input()
splitted_b = []
idx = 0
for word in a.split():
w_len = len(word)
splitted_b.append(b[idx:idx+w_len])
idx += w_len
print(splitted_b)
The idea is taking consecutive sub-strings from b of the length of each word on a.
Instead of using indices, you can iterate over each character of a. If the character is not a space, add the next character of b to your b_word. If it is a space, add b_word to the b_array
b_iter = iter(b) # Create an iterator from b so we can get the next character when needed
b_word = []
b_array = []
for char in a:
# If char is a space, and b_word isn't empty, append it to the result
if char == " " and b_word:
b_array.append("".join(b_word))
b_word = []
else:
b_word.append(next(b_iter)) # Append the next character from b to b_word
if b_word: # If anything left over in b_word, append it to the result
b_array.append("".join(b_word))
Which gives b_array = ['aaab', 'bbb']
Note that I changed b_word to a list that I .append to every time I add a character. This prevents the entire string from being recreated every time you append a character.
Then join all the characters using "".join(b_word) before appending it to b_array.
So to accomodate for any number of spaces in the input it gets a bit more complex as the indexes of the letters will change with each space that is added. So to gather all of the spaces in the string I created this loop which will account of the multiple spaces and alter the index with each new space in the initial word.
indexs = []
new = ''
for i in range(len(a)):
if len(indexs) > 0:
if a[i] == ' ':
indexs.append(i-len(indexs))
else:
if a[i] == ' ':
indexs.append(i)
Then we simple concatenate them together to create a new string that includes spaces at the predetermined indexes.
for i in range(len(b)):
if i in indexs:
print(i)
new += " "
new += b[i]
else:
new += b[i]
print(new)
Hope this helps.
Code
sone = input()
stwo = 'zzzzzxxxyyyyy'
nwz = []
wrd = ''
cnt = 0
idx = 0
spc = sone.split(' ') #split by whitespace
a = [len(i) for i in spc] #list word lengths w/out ws
for i in stwo:
if cnt == a[idx]: #if current iter eq. word length w/out ws
nwz.append(wrd) #append the word
wrd = '' #clear old word
wrd = wrd + i #start new word
idx = idx + 1
cnt = 0
else:
wrd = wrd + i #building word
cnt = cnt + 1
nwz.append(wrd) #append remaining word
print(nwz)
Result
>'split and match'
['zzzzz', 'xxx', 'yyyyy']

Python list index not in order

I'm trying to make it so that my text alternates between upper and lower case like the question ask. It seems to skip 3 in the indexing and I can't figure out why.
sentence = input("Write a sentence")
newList = []
for i in range(len(sentence)):
if sentence[i] != " ":
newList.append(sentence[i])
listJoint = "".join(newList)
newList2 = []
for i in range(len(listJoint)):
if (listJoint.index(listJoint[i]) % 2) == 0:
print(listJoint.index(listJoint[i]))
newList2.append(listJoint[i].upper())
elif (listJoint.index(listJoint[i]) % 2) != 0:
print(listJoint.index(listJoint[i]))
newList2.append(listJoint[i].lower())
print(newList2)
#newListJoint = "".join(newList2)
#print(newListJoint[::-1])
Thanks in advance
List index doesn't go 0 1 2 3 4
The function .index() finds the first occurrence of that letter. 'L' occurs at index 2 and 3 so it would return 2 for both L's.
Iterate through each character of the string and alternate upper/lower methods.
sentence = "Hello"
alternated_sentence = ''
for i, char in enumerate(sentence):
if i % 2:
alternated_sentence += char.upper()
else:
alternated_sentence += char.lower()
print(alternated_sentence)
#hElLo
sentence = input("Write a sentence:")
# Remove spaces (as per your question)
sentence = sentence.replace(' ', '')
# Reverse the string order (as per your question)
sentence = sentence[::-1]
result = []
for i in range(len(sentence)):
if(i%2==1):
result.append(sentence[i].lower())
else:
result.append(sentence[i].upper())
print(''.join(result))
Here's the solution. The above code would give output as follows:
Write a sentence: Hello world
DlRoWoLlEh
I never realised index method referenced the first instance of the character. This works:
sentence = input("Write a sentence")
newList = []
for i in range(len(sentence)):
if sentence[i] != " ":
newList.append(sentence[i])
listJoint = "".join(newList)
newList2 = []
for i, value in enumerate(newList):
if i % 2 == 0:
newList2.append(listJoint[i].upper())
elif i % 2 !=0:
newList2.append(listJoint[i].lower())
newListJoint = "".join(newList2)
print(newListJoint[::-1])

Getting output as character and it's occurrence in the string

The input I gave is like this
Input- abccdddeffg
and the output I want is character and it's occurrence number
Output- a1b1c2d3e1f2g1
my code
uni = []
string = 'abcccdd'
for i in range(0, len(string)):
for j in range(i+1, len(string)):
if (string[i] == string[j]):
uni.append(string[i])
for oc in uni:
cou= uni.count(oc)
print(oc,cou)
Thanks in advance
You can use the Counter from collections to get the count of every character in the list. Then use a forloop to generate your result and use set to make sure no character is repeated in the result.
from collections import Counter
string = "abccdddeffg"
counts = Counter(string)
sets = set()
result = []
for s in string:
if s not in sets:
result.append(f"{s}{counts[s]}")
sets.add(s)
result = ''.join(result)
print(result)
Firstly, your output contains an error, there should be 1 next to e as it was there in first occurring characters.
After clearing this, this is what you need:
import collections
s = "abccdddeffg" # your string
a = dict((letter,s.count(letter)) for letter in set(s))
a = collections.OrderedDict(sorted(a.items()))
answer = "" # to store the result
for i,j in zip(a.keys(),a.values()):
answer+= i + str(j)
print(answer)
answer will return:
'a1b1c2d3e1f2g1'
Here is a simpler approach:
def freq_string(string):
output, buffer = "", ""
for letter in string:
if buffer != letter:
buffer = letter
output += f"{letter}{string.count(letter)}"
return output
Note: this does assume that same characters are in succession rather than spread randomly around the string.
Assuming your input string is sorted and has at least one letter, you can do a simple loop to handle it:
if len(string) == 1:
# print out the only string and 1 as its occurences
print(string + '1')
else:
# initialize first string, its counter, and our result string
prev = string[0]
counter = 1
result = ''
# loop over each letter
for letter in string[1:]:
curr = letter
# if current letter is different from previous letter,
# concat the result and refresh our counter
# else just increase the counter
if curr != prev:
result = result + prev + str(counter)
counter = 1
else:
counter = counter + 1
prev = curr
# don't forget to handle the last case
result = result + prev + str(counter)
print(result)
in the simplest way:
inp = 'abccdddeffg'
l=[]
o=""
for i in inp:
if i in l:
pass
else:
l.append(i)
o+="{}{}".format(i,inp.count(i))
print(o)
output
'a1b1c2d3e1f2g1'

How to count specific substrings using slice notation

I want to count the number of occurrences of the substring "bob" within the string s. I do this exercise for an edX Course.
s = 'azcbobobegghakl'
counter = 0
numofiterations = len(s)
position = 0
#loop that goes through the string char by char
for iteration in range(numofiterations):
if s[position] == "b": # search pos. for starting point
if s[position+1:position+2] == "ob": # check if complete
counter += 1
position +=1
print("Number of times bob occurs is: " + str(counter))
However it seems that the s[position+1:position+2] statement is not working properly. How do i adress the two chars behind a "b"?
The second slice index isn't included. It means that s[position+1:position+2] is a single character at position position + 1, and this substring cannot be equal to ob. See a related answer. You need [:position + 3]:
s = 'azcbobobegghakl'
counter = 0
numofiterations = len(s)
position = 0
#loop that goes through the string char by char
for iteration in range(numofiterations - 2):
if s[position] == "b": # search pos. for starting point
if s[position+1:position+3] == "ob": # check if complete
counter += 1
position +=1
print("Number of times bob occurs is: " + str(counter))
# 2
You could use .find with an index:
s = 'azcbobobegghakl'
needle = 'bob'
idx = -1; cnt = 0
while True:
idx = s.find(needle, idx+1)
if idx >= 0:
cnt += 1
else:
break
print("{} was found {} times.".format(needle, cnt))
# bob was found 2 times.
Eric's answer explains perfectly why your approach didn't work (slicing in Python is end-exclusive), but let me propose another option:
s = 'azcbobobegghakl'
substrings = [s[i:] for i in range(0, len(s))]
filtered_s = filter(substrings, lambda s: s.startswith("bob"))
result = len(filtered_s)
or simply
s = 'azcbobobegghakl'
result = sum(1 for ss in [s[i:] for i in range(0, len(s))] if ss.startswith("bob"))

Python Word Count

wordsFreq = {}
words = []
while True:
inputWord = raw_input()
if (inputWord != ""):
words.append(inputWord)
else:
break
for word in words:
wordsFreq[word] = wordsFreq.get(word, 0) + 1
for word,freq in wordsFreq.items():
print word + " - " + str(freq)
Apparently my words[] and the for loop is redundant but I had no further explanation than that, can anyone explain to me why it is redundant?
You can skip the step of building a list of words and instead directly create the frequency dict as the user is entering words. I've used defaultdict to avoid having to check if a word has already been added.
from collections import defaultdict
wordsFreq = defaultdict(int)
while True:
word = raw_input()
if not word:
break
wordsFreq[word] += 1
If you aren't allowed to use defaultdict, it could look like this:
wordsFreq = {}
while True:
word = raw_input()
if not word:
break
wordsFreq[word] = wordFreq.get(word, 0) + 1
You can use collections.Counter to do this easily:
from collections import Counter
words = []
input_word = True
while input_word:
input_word = raw_input()
words.append(input_word)
counted = Counter(words)
for word, freq in counted.items():
print word + " - " + str(freq)
Note that an empty string evaluates to false, so rather than breaking when it equals an empty string, we can just use the string as our loop condition.
Edit: If you don't wish to use Counter as an academic exercise, then the next best option is a collections.defaultdict:
from collections import defaultdict
words = defaultdict(int)
input_word = True
while input_word:
input_word = raw_input()
if input_word:
words[input_word] += 1
for word, freq in words.items():
print word + " - " + str(freq)
The defaultdict ensures all keys will point to a value of 0 if they havn't been used before. This makes it easy for us to count using one.
If you still want to keep your list of words as well, then you would need to do that in addition. E.g:
words = []
words_count = defaultdict(int)
input_word = True
while input_word:
input_word = raw_input()
if input_word:
words.append(input_word)
words_count[input_word] += 1
I think your teacher was trying to say you can write the loop like this
wordsFreq = {}
while True:
inputWord = raw_input()
if (inputWord != ""):
wordsFreq[inputWord] = wordsFreq.get(inputWord, 0) + 1
else:
break
for word,freq in wordsFreq.items():
print word + " - " + str(freq)
There is no need to store the words in a temporary list, you can count them as you read them in
You can do this:
wordsFreq = {}
while True:
inputWord = raw_input()
try:
wordsFreq[inputWord] = wordsFreq[inputWord] + 1
except KeyError:
wordsFreq[inputWord] = 1

Categories