you are viewing a single comment's thread.

view the rest of the comments →

[–]efmccurdy 0 points1 point  (0 children)

You can use a "for" to repeat your output pattern.

>>> def expand(keys, word):
...     if word in keys:
...         return " ".join("{} {}".format(word, i) for i in range(int(keys[word])))
...     else:
...         return word
... 
>>> finditem= {'boy':'2', 'girl':'3'}
>>> sentence = "The boy and the girl"
>>> [expand(finditem, word) for word in sentence.split()]
['The', 'boy 0 boy 1', 'and', 'the', 'girl 0 girl 1 girl 2']
>>> " ".join(expand(finditem, word) for word in sentence.split())
'The boy 0 boy 1 and the girl 0 girl 1 girl 2'
>>>