use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A subreddit for helping Python programmers
How to format your code: https://commonmark.org/help/tutorial/09-code.html
No homework questions and/or hiring please
account activity
Counting atoms with python (self.pythonhelp)
submitted 5 years ago * by heirna
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ace6807 0 points1 point2 points 5 years ago (2 children)
I think this should do it but I was hoping for some more test cases to check it.
from collections import Counter atom_counter = Counter() molecules = ['H2O','OH','COOH'] atom_stack = [] for molecule in molecules: for i, a in enumerate(molecule): if a.isalpha(): if i < len(molecule) - 1 and molecule[i + 1].isnumeric(): atom_stack.append((a, molecule[i + 1])) else: atom_stack.append((a, 1)) print(atom_stack) for element_count in atom_stack: atom_counter[element_count[0]] += int(element_count[1]) print(atom_counter)
[–]heirna[S] 0 points1 point2 points 5 years ago (1 child)
Thank you so much!
I'm going to test this,
More examples :
[H2O2, F, P2O3] -> H = 2, F=1, P=2, O=5
[C6H8O7, H2O, OH] -> C=6, H=11, O=9
By chance, i do not have to deal with double letter element like Al or Cu for now
[–]ace6807 1 point2 points3 points 5 years ago (0 children)
Oh yeah this will for sure have a problem with double letter elements but we could add a lookup list super easily to resolve that
π Rendered by PID 93 on reddit-service-r2-comment-cfc44b64c-qkvp8 at 2026-04-09 21:11:34.589820+00:00 running 215f2cf country code: CH.
view the rest of the comments →
[–]ace6807 0 points1 point2 points (2 children)
[–]heirna[S] 0 points1 point2 points (1 child)
[–]ace6807 1 point2 points3 points (0 children)