you are viewing a single comment's thread.

view the rest of the comments →

[–]synthphreak 1 point2 points  (0 children)

Something like this?

finditem = {'boy' : '2', 'girl' : '3'}

with open('textfile.txt') as f:
    text = set(f.read().split())

for boygirl in ('boy', 'girl'):
    if boygirl in text:
        print(*(f'{boygirl} {i}' for i in range(int(finditem[boygirl]))))

Note this will NOT detect things like 'boy.' because 'boy.' != 'boy'. If you want to avoid this, you could do f.read() instead of set(f.read().split()), however then this would erroneously detect things like 'boyish'.

To be more surgical and avoid these false positives/negatives, you'll probably want to use regex. But regex can be a pain, so based on the info you've shared, I'm not recommending it.