Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8438105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:36:57+00:00 2026-06-10T07:36:57+00:00

In a previous question , it was suggested that, in order to divide a

  • 0

In a previous question, it was suggested that, in order to divide a string and store it, I should use a list, like so:

[a for a in re.split(r'([A-Z][a-z]*)', 'MgSO4') if a]
['Mg', u'S', u'O', u'4']

What I’d like to ask this time around is how would I be able to use that to store the different strings created into variables so I can look them up in the CSV file I have, if it’s at all possible. Where it says ‘MgSO4’ would be coming from a variable called ‘formula’, which is produced from a raw_input, like so:

formula = raw_input("Enter formula: ")

Full program code can be found here, and I’ve included the more relevant part below. Thanks in advance for any help!

formula = raw_input("Enter formula: ")


[a for a in re.split(r'([A-Z][a-z]*)', 'MgSO4') if a]

weight_sum = sum(float(formul_data.get(elem.lower())) for elem in elements)
print "Total weight =", weightSum
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:36:59+00:00Added an answer on June 10, 2026 at 7:36 am

    If your goal is to be able to add up the molecular weights of the atoms comprising a molecule, I suggest doing your regular expressions a bit differently. Instead of having the numbers mixed in with the element symbols in your split list, attach them to the preceding element instead (and attach a 1 if there was no number). Here’s how I’d do that:

    import re
    
    # a partial table of atomic weights, replace with something better!
    weights = { "H" : 1,  "Na": 11, "Mg": 12, "C" : 12, "N" : 14,
                "O" : 16, "F" : 19, "Al": 27, "S" : 32, "Cl": 35, 
          }
    
    def molecularWeight(formula):
        matches = re.findall(r"([A-Z][a-z]?)([0-9]*)", formula)
        return sum(weights[symbol] * (int(count) if count else 1)
                   for (symbol, count) in matches)
    

    To make this fit with the code you’ve shown, replace weights[symbol] with something like formul_data.get(symbol.lower(), 0) (or whatever is necessary to get appropriate atomic weights by symbol in your code).

    This should handle any empiric formula, and many structural ones, as long as there are no parentheses. To solve fully parenthesized formulas you’ll need to make a better parser, as simple regular expressions won’t work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In answer to a previous question , several people suggested that I use BeautifulSoup
My previous question broght up some discussion which suggested that I am wrong to
In a previous question I asked, the suggested answer was for me to partition
This continues from a previous question . I tried the suggested fix to check
i've tried what was suggested by a previous Stackoverflow question about how to ignore
In previous question of mine, someone had meantioned that using Semaphores were expensive in
This question is extended part of my previous question, Finding number position in string
This question is related to a previous question of mine That's my current code
This question follows on from a previous question, that has raised a further issue.
New question As suggested by Asaph in previous question: Regex to check if exact

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.