I need a count of all the emails in a list, some of the emails however are consolidated together with a | symbol. These need to be split and the emails need to be counted after splitting to avoid getting an inaccurate or low count of frequencies.
I have a list that is something like this:
test = ['[email protected]', '[email protected]|[email protected]', '[email protected]|[email protected]', '[email protected]', '[email protected]']
I performed a set of operations to split and when I split, the pipe gets replaced by double quotes at that location so I replace the double with single quotes so I have all email ids enclosed in single quotes.
# convert list to a string
test_str = str(test)
# apply string operation to split by separator '|'
test1 = test_str.split('|')
print(test1)
--> OUTPUT of above print statement: ["['[email protected]', '[email protected]", "[email protected]', '[email protected]", "[email protected]', '[email protected]', '[email protected]']"]
test2 = str(test1)
test3 = test2.replace('"','')
print(test3)
--> OUTPUT of above print statement: [['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']]
How can I now obtain a count of all the emails? This is a string essentially and if it's a list, I could use collections.Counter to easily obtain a count.
I'd like to get a list like the one listed below that has the email and the count in descending order of frequency
['[email protected]': 3, '[email protected]': 2, '[email protected]': 1, '[email protected]': 1]
Thanks for the help!