Iterate Over Elements Of Strings At The Same Time
I have a dictionary containing sentences that is keyed by the book and the page from which it came from: # lists to build dictionary - for reproducibility pages = [12, 41, 5
Solution 1:
I think that what you need is a better data structure, that would let you to retrieve the book/page from the sentence. There are many possible designs. This is what I would do:
First, Create a data structure that holds a sentence, along with its book/page:
classSentenceWithMeta(object):
def__init__(self, sentence):
self.sentence = sentence
self.book = None
self.page = None
Then, hold all your sentences. For example:
sentences_with_meta = [SentenceWithMeta(sentence) for sentence in sentences]
At this point, initialize sentences_with_meta fields book and page fields:
# Make dictionary
sentences_with_meta = [SentenceWithMeta(sentence) for sentence in sentences]
for i inrange(len(pages)):
book = bookCodes[i]
page = pages[i]
sentence_with_meta = sentences_with_meta[i]
sentence_with_meta.book = book
sentence_with_meta.page = page
Finally, in the wordStopper method, work with sentences_with_meta array, the following way:
defwordStopper(sentences):
random.shuffle(sentences_with_meta)
vowels = dict.fromkeys(['A', 'E', 'I', 'O', 'U'], 10)
for i inrange(len(sentences[1])):
for swm in sentences_with_meta:
try:
l = swm.sentence[i:i + 1]
...
# the rest of the code is the same. You return swm, which has the book# and page already in the structure.
Side node: To get letter i from a string, you don't need to use slice. Just use the index reference:
l = swm.sentence[i]
There are many many other designs that would work as well.
Post a Comment for "Iterate Over Elements Of Strings At The Same Time"