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 8664901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:21:07+00:00 2026-06-12T17:21:07+00:00

I have string with some equation, that include variables. For example: eq='(a+1)*c-d’ Also I

  • 0

I have string with some equation, that include variables. For example:

eq='(a+1)*c-d'

Also I have dictionary with values for variables. For example:

val={'a':5,'c':'k','d':'300n'}

As You can see in same cases value of variable can be other variable, and in some cases not all variables from eq will be in val.

I need to replace all variables in eq with values from val.
Of cause straight forward code will be something like:

for v in val.keys():
  eq=re.sub(v,val[v],eq)

But this code will fail in some cases. For example when in variables list will be variables “n” and “nn”

So, can someone think about better solution?
Thank you.

  • 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-12T17:21:08+00:00Added an answer on June 12, 2026 at 5:21 pm

    Try:

    for k,v in val.items():
        eq = re.sub(r'\b' + k + r'\b', v, eq)
    

    This will search for variables by their full name, i.e. if the val = {'x': '5'} and the equation contains substrings like “xx”, “x2”, or even “2x”, these strings will not be replaced.

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

Sidebar

Related Questions

I have some String variables which are getting the values from invoking the function
I have some String[] arrays, for example: ['a1', 'a2'] ['b1', 'b2', 'b3', 'b4'] ['c1']
Simple example: we have string Some sample string Of Text. And I want to
I have some string example : String1. Today is beautiful day String2. I love
I have a string 'some.file.name',I want to grab 'some.file'. To do that,I need to
I have some string that have a type like this NSString *string1 = @/Users/mine/Library/Application
I have some string values in a dataset. In my composite control, in RenderContent
I have some String values like this format, [INFO] [Tue Aug 21 14:54:22 2012]
if I have a string like some/unknown/amount/of/sub/folder/file.txt how can I get only the the
I have this string: 467:some-text-here-1786 How can I select only the first numerical value

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.