Finding the indices of all matches in a string, appending search term and indices to a dictionary.

12345678910111213

I think I have an infinite loop? I made a dictionary with search terms as keys and one index where those keys were found in my_string. I'd like to create a search_dict with a list of ALL matches in my_string listed as indices for each key as a search term.

My search_dict isn't populated except for one item which has millions of items.

my_string='Shall I compare thee to a summer\'s day?'
#string_dict has only a single index as a value where its key was found in my_string
string_dict={'a': 36, ' ': 34, 'e': 30, '': 39, 'h': 17, 'm': 29, 'l': 4, 'o': 22, 'e ': 19, 's': 33, 'r': 31, 't': 21, ' t': 20, 'e t': 19}

#I'd like search_dict to have all indices for key matches in my_string
search_dict=dict()
for key in string_dict:
    search_dict[key]=list()
for item in search_dict:
    start=0
    end=len(my_string)
    found=my_string.find(item,start,end)
    while start<end:
        if found>=0:
            search_dict[key].append(found)
            start=found+len(item)
            found=my_string.find(item,start,end)
        else:
            break
print search_dict

I've also tried the changes below. Still not sure why if my_string.find comes up -1 (not found) the loop isn't breaking for the next search key iteration.

        else:
            break
#with
        if found<0:
            break
Slick

I'm thinking that if you're looking for substrings and not characters I think regex would work best.

>>> import re
>>> my_string='Shall I compare thee to a summer\'s day?'
>>> search_items = ['a', ' ', 'e', 'h', 'm', 'l', 'o', 'e ', 's', 'r', 't', ' t', 'e t']
>>> results_dict = {}
>>> for search_item in search_items:
...     results_dict[search_item] = [m.start() for m in re.finditer(search_item, my_string)]
... 
>>> for elem in results_dict:
...     print("%s: %s" % (elem, results_dict[elem]))
... 
a: [2, 12, 24, 36]
 : [5, 7, 15, 20, 23, 25, 34]
e: [14, 18, 19, 30]
h: [1, 17]
m: [10, 28, 29]
l: [3, 4]
o: [9, 22]
e : [14, 19]
s: [26, 33]
r: [13, 31]
t: [16, 21]
 t: [15, 20]
e t: [14, 19]

While it's not specified in your question the value in the results is the starting position of the substring.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find all indices of a search term in a string

From Dev

Finding all indices by ismember

From Dev

Finding all indices by ismember

From Dev

Recursively finding indices of a string

From Dev

Recursively finding indices of a string

From Dev

Get indices of all character elements matches in string in R

From Dev

Finding indices of matches of one array in another array

From Dev

Finding indices for all instances of element in array

From Dev

Find all indices of pattern in string?

From Dev

Find all indices of a value in a dictionary python

From Dev

string indices must be integers not str dictionary

From Dev

Finding indices of elements in vector

From Dev

find all indices of the pattern in the input string

From Dev

Elegant way of finding all integer element indices in a MATLAB array?

From Dev

Elegant way of finding all integer element indices in a MATLAB array?

From Dev

Finding a sub-string with indices from both ends

From Dev

Finding the number of matching letters in two different string at the same indices

From Dev

String indices must be integers, not string - retrieving value from dictionary

From Dev

finding specific indices with pointer array

From Dev

Finding indices of element in array ruby

From Dev

Finding Indices of Unique Elements in R

From Dev

Finding the column indices of submatrices in MATLAB

From Dev

Python dictionary search not finding result given a string

From Dev

Elasticsearch counts of all indices

From Dev

Appending list B with indices from enumerated list A

From Dev

String indices in Swift 2

From Dev

String indices must be integer - they are

From Dev

Find Certain String Indices

From Dev

String Indices Swift 3

Related Related

HotTag

Archive