find the index of a letter in a string in python - string

I tried to print the index of every i in the word Mississippi. I've got the result but the print statement is repeating 3 times. This is the code
s="Mississippi"
start=0
while start<len(s):
print "the index of i is: ", s.find('i',start,len(s))
start=start+1

If you use enumerate then you iterate through the string, looking at each letter and idx is counting upwards as you go.
for idx, letter in enumerate(s):
if letter == "i":
print "the index of i is: ", idx

do you want to print the indexes as a list? try this:
l = []
for index, char in enumerate('mississippi'):
if char == 'i':
l.append(index)
print "the index of i is: ", l
the result will be:
the index of i is: [1, 4, 7, 10]

Because of the while loop, you print the last found position of "i" in each run.
I would prefer a for loop over the string:
s="Misssssissippi"
start=0
character="i"
for i in s:
if (i == character):
print "the index of i is: ", start
start=start+1

import re
s="Wisconsin"
for c in re.finditer('i',s):
print c.start(0)

Related

How can i print every second letter first and then starting word in Python 3. eg my string = "stackoverflow" iwant result as "tscavorelfwo"

I want to print every next letter before first letter.
string = "stackoveerflow"
for i in len((string)-1):
print(i)
a = string[i+1] + string[i]
print(a)
One simple approach would be the following (recursive) function:
def reverse_chunks(s, n=2):
if not s:
return s
return s[:n][::-1] + reverse_chunks(s[n:])
>>> reverse_chunks("stackoverflow")
'tscaokevfrolw'
Of course, there are more performant iterative approaches.

Count the no of occurence of a each string and print the eac number and string side by side using python

USING PYTHON3
So this is my question, say I have a string
my_str="aaaaabbbbcccdde"
I want to write a code that gives me an output as
5a4b3c2d1e
I saw a code in the book automate the boring stuff that does something similar but not exactly.
This is the code below:
my_str="aaaaabbbbcccdde"
count = {}
for character in my_str:
count.setdefault(character, 0)
count[character]+= 1
print(count)
for item in count:
print(count[item], item)
and this is the output it gives me:
{'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1}
5 a
4 b
3 c
2 d
1 e
not
5a4b3c2d1e
which is the output I want.
Help anyone? (:
You can do something like this brother by modifying your own code's output. Happy coding!
my_str="aaaaabbbbcccdde"
count = {}
for character in my_str:
count.setdefault(character, 0)
count[character]+= 1
string_to_print = ""
for item in count:
string_to_print += str(count[item])+item
print(string_to_print)
Output:
5a3c4b1e2d
You could do:
count = {}
for char in my_str:
count[char] = count.get(char,0) + 1
for item in count:
print(count[item],item,sep='', end='')
5a4b3c2d1e
if you need a one liner, you could do:
''.join([str(my_str.count(i)) + i for i in sorted(set(my_str),key = my_str.index)])
Out: '5a4b3c2d1e'
Using re:
import re
my_str="aaaaabbbbcccdde"
res=eval(re.sub(r"(.)(\1*)", r"+str(len('\2\1'))+'\1'", my_str)[1:])
Output:
5a4b3c2d1e
Caveat
It aggregates consecutive chars only i.e.
import re
my_str="aaaaabbbbcccddea"
res=eval(re.sub(r"(.)(\1*)", r"+str(len('\2\1'))+'\1'", my_str)[1:])
#outputs:
>> 5a4b3c2d1e1a
You were close. All you need to do to complete the code is to concat count[item] and item together and add it to the list. Then join all list elements into a final string.
my_str = "aaaaabbbbcccdde"
count = {}
list_1 = []
for character in my_str:
count.setdefault(character, 0)
count[character] += 1
print(count)
for item in count:
new_item = str(count[item]) + item
list_1.append(new_item)
final_string = ''.join(list_1)
print(final_string)
Output
5a4b3c2d1e
Or if you don't want to make another list you can just make an empty string variable and concat to that directly
my_str = "aaaaabbbbcccdde"
final_string = ""
count = {}
for character in my_str:
count.setdefault(character, 0)
count[character] += 1
print(count)
for item in count:
final_string += str(count[item]) + item
print(final_string)
Output
5a4b3c2d1e

replace an occurrence with a distinct number every loop

The first occurrence of the character in the string will be replaced with a 1, the second occurrence with a 2, etc.
ive tried using for loop and the max function to replace the last occurence but it doesnt seem to work.
string=str(input('string: '))
x=input('character: ')
list=[]
for i in range(len(string)):
if string[i]==x:
list.append(i)
Z=str(max(list))
print(string.replace(x,Z,[::-1]))
the output should be as following
string: departmentofcomputerscience
character: e
d1partm2ntofcomput3rsci4nc5
Here's a way to do it.
Use a counter for each character in the loop, and store values in the list, then merge the list. Use the current value if not equal to the character, counter otherwise:
string=str(input('string: '))
x=input('character: ')
# Use list to store results and a counter
l = []
counter = 0
for c in string:
if c==x:
counter += 1
l.append(str(counter))
else:
l.append(c)
# Merge the resulting list into string
res = "".join(l)
# Output the result
print(res)
For the input string: departmentofcomputerscience
and the character: e
The output is
d1partm2ntofcomput3rsci4nc5
Here is another way to achieve the goal using a list and the method replace():
string = str(input('string: '))
x = input('character: ')
list = []
for i in range(len(string)):
if string[i] == x:
list.append(i) # add all indexes to replace to the list
if len(list) > 0:
j = 0
for i in range(len(list)):
j += 1
string = string.replace(string[list[i]], str(j), 1) # replace the element once at time
print(string)
For string: departmentofcomputerscience
character: e
Output: d1partm2ntofcomput3rsci4nc5
def replace(s, c):
'''
#parameter s: input string
#parameter c: input character to be replaced
#return s: where every occurence of c is
replaced by it's nth occurence
'''
so = list(s)
j = 1
for i in range(len(so)):
if so[i] == c:
so[i] = str(j)
j = j + 1
return ''.join(so)

number of occurrences of list of words in a string with O(n)

I have already seen this answer to a similar question:
https://stackoverflow.com/a/44311921/5881884
Where the ahocorasick algorithm is used to show if each word in a list exists in a string or not with O(n). But I want to get the frequency of each word in a list in a string.
For example if
my_string = "some text yes text text some"
my_list = ["some", "text", "yes", "not"]
I would want the result:
[2, 3, 1, 0]
I did not find an exact example for this in the documentation, any idea how to accomplish this?
Other O(n) solutions than using ahocorasick would also be appreciated.
Implementation:
Here's an Aho-Corasick frequency counter:
import ahocorasick
def ac_frequency(needles, haystack):
frequencies = [0] * len(needles)
# Make a searcher
searcher = ahocorasick.Automaton()
for i, needle in enumerate(needles):
searcher.add_word(needle, i)
searcher.make_automaton()
# Add up all frequencies
for _, i in searcher.iter(haystack):
frequencies[i] += 1
return frequencies
(For your example, you'd call ac_frequency(my_list, my_string) to get the list of counts)
For medium-to-large inputs this will be substantially faster than other methods.
Notes:
For real data, this method will potentially yield different results than the other solutions posted, because Aho-Corasick looks for all occurrences of the target words, including substrings.
If you want to find full-words only, you can call searcher.add_word with space/punctuation-padded versions of the original string:
...
padding_start = [" ", "\n", "\t"]
padding_end = [" ", ".", ";", ",", "-", "–", "—", "?", "!", "\n"]
for i, needle in enumerate(needles):
for s, e in [(s,e) for s in padding_start for e in padding_end]:
searcher.add_word(s + needle + e, i)
searcher.make_automaton()
# Add up all frequencies
for _, i in searcher.iter(" " + haystack + " "):
...
The Counter in the collections module may be of use to you:
from collections import Counter
my_string = "some text yes text text some"
my_list = ["some", "text", "yes", "not"]
counter = Counter(my_string.split(' '))
[counter.get(item, 0) for item in my_list]
# out: [2, 3, 1, 0]
You can use list comprehensions to count the number of times the specific list occurs in my_string:
[my_string.split().count(i) for i in my_list]
[2, 3, 1, 0]
You can use a dictionary to count the occurrences of the words you care about:
counts = dict.fromkeys(my_list, 0) # initialize the counting dict with all counts at zero
for word in my_string.split():
if word in counts: # this test filters out any unwanted words
counts[word] += 1 # increment the count
The counts dict will hold the count of each word. If you really do need a list of counts in the same order as the original list of keywords (and the dict won't do), you can add a final step after the loop has finished:
results = [counts[word] for word in my_list]

Find and print vowels from a string using a while loop

Study assignment (using python 3):
For a study assignment I need to write a program that prints the indices of all vowels in a string, preferably using a 'while-loop'.
So far I have managed to design a 'for-loop' to get the job done, but I could surely need some help on the 'while-loop'
for-loop solution:
string = input( "Typ in a string: " )
vowels = "a", "e", "i", "o", "u"
indices = ""
for i in string:
if i in vowels:
indices += i
print( indices )
while-loop solution:
string = input( "Typ in a string: " )
vowels = "a", "e", "i", "o", "u"
indices = ""
while i < len( string ):
<code>
i += 1
print( indices )
Would the use 'index()' or 'find()' work here?
Try This :
string = input( "Typ in a string: " )
vowels = ["a", "e", "i", "o", "u"]
higher_bound=1
lower_bound=0
while lower_bound<higher_bound:
convert_str=list(string)
find_vowel=list(set(vowels).intersection(convert_str))
print("Vowels in {} are {}".format(string,"".join(find_vowel)))
lower_bound+=1
You can also set higher_bound to len(string) then it will print result as many times as len of string.
Since this is your Study assignment you should look and practice yourself instead of copy paste. Here is additional info for solution :
In mathematics, the intersection A ∩ B of two sets A and B is the set
that contains all elements of A that also belong to B (or
equivalently, all elements of B that also belong to A), but no other
elements. For explanation of the symbols used in this article, refer
to the table of mathematical symbols.
In python :
The syntax of intersection() in Python is:
A.intersection(*other_sets)
A = {2, 3, 5, 4}
B = {2, 5, 100}
C = {2, 3, 8, 9, 10}
print(B.intersection(A))
print(B.intersection(C))
print(A.intersection(C))
print(C.intersection(A, B))
You can get the character at index x of a string by doing string[x]!
i = 0 # initialise i to 0 here first!
while i < len( string ):
if string[i] in vowels:
indices += str(i)
i += 1
print( indices )
However, is making indices a str really suitable? I don't think so, since you don't have separators between the indices. Is the string "12" mean that there are 2 vowels at index 1 and 2, or one vowel index 12? You can try using a list to store the indices:
indices = []
And you can add i to it by doing:
indices.append(i)
BTW, your for loop solution will print the vowel characters, not the indices.
If you don't want to use lists, you can also add an extra space after each index.
indices += str(I) + " "

Resources