How can I sum the two string scores in the output and type them in a single line without repeating the same domain?
My code:
score_dict = {"redapple": "30",
"greenapple": "50",
"red": "5"}
results = []
with open("domain.txt", 'r') as g:
for domain in g.readlines():
hit = None
for substring, score in score_dict.items():
for hit in score_dict:
if substring in domain:
if domain == domain:
hit = True
results.append({'domain': domain.strip(), 'substring': substring, 'score': score})
break
if not hit:
results.append({'domain': domain.strip(), 'substring': substring, 'score': 0})
with open("score_result.txt", "w") as file:
for item in results:
file.write("%s\n" % item)
OUTPUT:
{'domain': 'redgreenapple.com', 'substring': 'red', 'score': '5'}
{'domain': 'redgreenapple.com', 'substring': 'greenapple', 'score': '50'}
That's what I want redgreenapple.com
the domain can print the total score
in a single line
without repeating
it.
The output I want:
{'domain': 'redgreenapple.com', 'substring': 'red, greenapple', 'score': '55'}
OR:
{'domain': 'redgreenapple.com', 'substring': 'red, 'substring': 'greenapple, 'score': '55'}
This code should do what you need. Instead of using a list I'm using a dictionary to keep the results for each domain. To add the scores you just need to convert to numbers and back to string after sum
. I'm also keeping the substrings in a list and then joining them at the end.
score_dict = {"redapple": "30",
"greenapple": "50",
"red": "5"}
results = dict()
with open("domain.txt", 'r') as g:
for domain in g.readlines():
domain = domain.strip()
if domain not in results:
results[domain] = {'domain': domain, 'substring': [], 'score': 0}
for substring, score in score_dict.items():
if substring in domain:
results[domain]['substring'].append(substring)
results[domain]['score'] += int(score)
results[domain]['substring'] = ', '.join(results[domain]['substring'])
results[domain]['score'] = str(results[domain]['score'])
Check substring to find if it matched anything.
with open("score_result.txt", "w") as file:
for domain in results:
if results[domain]['substring']: # If there are no matches it will be ''
file.write("%s\n" % results[domain])