Separating strings with lists










3















I was trying to find a way to separate strings in a project of my called 'Chemistry Calculator'. This project takes strings from an input() and compare it in a list:



 substance1 = input('Substance 1: ')
substance2 = input('Substance 2: ')
elements = ['f','o','cl','br','i','s','c']

def affinity_table(element1:str,element2:str,table:list) -> str:
s = element1.lower()
r = element2.lower()
if s in table and r in table:

if table.index(s) < table.index(r):
print(s," will chage with ", r)

else:
print(s," won't change with ", r)

else:
print("Those substances are't in the list")


This code above works well.



So I wanted to have it working with hole substances and not just the element. To do this I need to separate the substance in to parts:



  • the cations parts

  • the anions parts.

Then I need to compare them with the list. I noticed that the contains() function showed exactly what I wanted, but only with one comparison.



My question came from:
Is there a way of using the contains() function with more than one string and then separate the string in to where the similarity is found.



Something similar to this:



a = 'NaCO3' #First input.
b = 'KCO3' #Second input.
list = ['Na','K'] #The list.

# Way of separating the values with the list.
# ^ my objective.
a1 = 'Na' #Separation with a.
a2 = 'CO3' #The rest of a.
b1 = 'K' #The rest of b.
b2 = 'CO3' #The rest of b.
# ^ expected outputs from the separation.
if table.index(a1) < table.index(a2):
print(a1,' will change with ', b1, 'and become', a1 + b2)

else:
print(a1," won't change with ", b1, 'and will stay normal')
# ^ the list index comparison from the 1st code.


#After the solution, here are the results:
The Results










share|improve this question




























    3















    I was trying to find a way to separate strings in a project of my called 'Chemistry Calculator'. This project takes strings from an input() and compare it in a list:



     substance1 = input('Substance 1: ')
    substance2 = input('Substance 2: ')
    elements = ['f','o','cl','br','i','s','c']

    def affinity_table(element1:str,element2:str,table:list) -> str:
    s = element1.lower()
    r = element2.lower()
    if s in table and r in table:

    if table.index(s) < table.index(r):
    print(s," will chage with ", r)

    else:
    print(s," won't change with ", r)

    else:
    print("Those substances are't in the list")


    This code above works well.



    So I wanted to have it working with hole substances and not just the element. To do this I need to separate the substance in to parts:



    • the cations parts

    • the anions parts.

    Then I need to compare them with the list. I noticed that the contains() function showed exactly what I wanted, but only with one comparison.



    My question came from:
    Is there a way of using the contains() function with more than one string and then separate the string in to where the similarity is found.



    Something similar to this:



    a = 'NaCO3' #First input.
    b = 'KCO3' #Second input.
    list = ['Na','K'] #The list.

    # Way of separating the values with the list.
    # ^ my objective.
    a1 = 'Na' #Separation with a.
    a2 = 'CO3' #The rest of a.
    b1 = 'K' #The rest of b.
    b2 = 'CO3' #The rest of b.
    # ^ expected outputs from the separation.
    if table.index(a1) < table.index(a2):
    print(a1,' will change with ', b1, 'and become', a1 + b2)

    else:
    print(a1," won't change with ", b1, 'and will stay normal')
    # ^ the list index comparison from the 1st code.


    #After the solution, here are the results:
    The Results










    share|improve this question


























      3












      3








      3








      I was trying to find a way to separate strings in a project of my called 'Chemistry Calculator'. This project takes strings from an input() and compare it in a list:



       substance1 = input('Substance 1: ')
      substance2 = input('Substance 2: ')
      elements = ['f','o','cl','br','i','s','c']

      def affinity_table(element1:str,element2:str,table:list) -> str:
      s = element1.lower()
      r = element2.lower()
      if s in table and r in table:

      if table.index(s) < table.index(r):
      print(s," will chage with ", r)

      else:
      print(s," won't change with ", r)

      else:
      print("Those substances are't in the list")


      This code above works well.



      So I wanted to have it working with hole substances and not just the element. To do this I need to separate the substance in to parts:



      • the cations parts

      • the anions parts.

      Then I need to compare them with the list. I noticed that the contains() function showed exactly what I wanted, but only with one comparison.



      My question came from:
      Is there a way of using the contains() function with more than one string and then separate the string in to where the similarity is found.



      Something similar to this:



      a = 'NaCO3' #First input.
      b = 'KCO3' #Second input.
      list = ['Na','K'] #The list.

      # Way of separating the values with the list.
      # ^ my objective.
      a1 = 'Na' #Separation with a.
      a2 = 'CO3' #The rest of a.
      b1 = 'K' #The rest of b.
      b2 = 'CO3' #The rest of b.
      # ^ expected outputs from the separation.
      if table.index(a1) < table.index(a2):
      print(a1,' will change with ', b1, 'and become', a1 + b2)

      else:
      print(a1," won't change with ", b1, 'and will stay normal')
      # ^ the list index comparison from the 1st code.


      #After the solution, here are the results:
      The Results










      share|improve this question
















      I was trying to find a way to separate strings in a project of my called 'Chemistry Calculator'. This project takes strings from an input() and compare it in a list:



       substance1 = input('Substance 1: ')
      substance2 = input('Substance 2: ')
      elements = ['f','o','cl','br','i','s','c']

      def affinity_table(element1:str,element2:str,table:list) -> str:
      s = element1.lower()
      r = element2.lower()
      if s in table and r in table:

      if table.index(s) < table.index(r):
      print(s," will chage with ", r)

      else:
      print(s," won't change with ", r)

      else:
      print("Those substances are't in the list")


      This code above works well.



      So I wanted to have it working with hole substances and not just the element. To do this I need to separate the substance in to parts:



      • the cations parts

      • the anions parts.

      Then I need to compare them with the list. I noticed that the contains() function showed exactly what I wanted, but only with one comparison.



      My question came from:
      Is there a way of using the contains() function with more than one string and then separate the string in to where the similarity is found.



      Something similar to this:



      a = 'NaCO3' #First input.
      b = 'KCO3' #Second input.
      list = ['Na','K'] #The list.

      # Way of separating the values with the list.
      # ^ my objective.
      a1 = 'Na' #Separation with a.
      a2 = 'CO3' #The rest of a.
      b1 = 'K' #The rest of b.
      b2 = 'CO3' #The rest of b.
      # ^ expected outputs from the separation.
      if table.index(a1) < table.index(a2):
      print(a1,' will change with ', b1, 'and become', a1 + b2)

      else:
      print(a1," won't change with ", b1, 'and will stay normal')
      # ^ the list index comparison from the 1st code.


      #After the solution, here are the results:
      The Results







      python python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 '18 at 16:58







      Locochoco

















      asked Nov 11 '18 at 16:00









      LocochocoLocochoco

      187




      187






















          1 Answer
          1






          active

          oldest

          votes


















          2














          Disclaimer



          Just to be clear: for the constrained scope of what you are doing this solution might be applicable. If you want to parse any chemical compound (and those can look quite complicated) you need a full fledged parser, not the toy regex solution I came up with.




          Here's an idea:



          Dynamically build a regex with elements from your list as alternating matching groups. (re.split keeps groups when splitting.)



          >>> import re
          >>> lst = ['Na', 'K']
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> regex
          >>> '(Na)|(K)'


          Apply the regex...



          >>> re.split(regex, 'NaCO3')
          >>> ['', 'Na', None, 'CO3']
          >>> re.split(regex, 'KCO3')
          >>> ['', None, 'K', 'CO3']


          ... and filter out falsy values (None, '')



          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['Na', 'CO3']
          >>> list(filter(None, re.split(regex, 'KCO3')))
          >>> ['K', 'CO3']


          You can assign to those values with extended iterable unpacking:



          >>> b1, b2, *unexpected_rest = filter(None, re.split(regex, 'KCO3'))
          >>> b1
          >>> 'K'
          >>> b2
          >>> 'CO3'


          If you want to bias the split in favor of longer matches, sort lst in descending order first.



          Not good:



          >>> lst = ['N', 'Na', 'CO3']
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['N', 'a', 'CO3']


          Better:



          >>> lst = ['N', 'Na', 'CO3']
          >>> lst = sorted(lst, key=len, reverse=True)
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['Na', 'CO3']


          Let me know if that works for you.






          share|improve this answer




















          • 1





            One potential problem is that most single letter element symbols can be mistakenly matched when there's a different element with a symbol that starts with the same letter. For example Nitrogen (N) / Sodium (Na) or Oxygen (O) and Osmium (Os). Maybe use a negative lookahead (Na|O|N)(?![a-z]) works?

            – Håken Lid
            Nov 11 '18 at 16:27












          • Man this seams very good, but just one question, what exactly the re function does? And the above guy is right, there is a way to differentiate them? (Basically that was the reason I choose to do a list comparison where all the variables follows the 'Chemistry Grammar').

            – Locochoco
            Nov 11 '18 at 16:31







          • 1





            re is the python standard library regular expressions module. docs.python.org/3.7/library/re.html

            – Håken Lid
            Nov 11 '18 at 16:33






          • 1





            I now that the bigger the compound the crazier the code must be, but for now I'm just starting to see what I can accomplish with stuff as simple as NaOH or KOH and your code was perfect to this b'cause I can even do this to the rest of the compound and make it (even in its simplicity) more "chemistry correct".

            – Locochoco
            Nov 11 '18 at 17:04






          • 1





            @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)

            – timgeb
            Nov 11 '18 at 17:05










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250525%2fseparating-strings-with-lists%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Disclaimer



          Just to be clear: for the constrained scope of what you are doing this solution might be applicable. If you want to parse any chemical compound (and those can look quite complicated) you need a full fledged parser, not the toy regex solution I came up with.




          Here's an idea:



          Dynamically build a regex with elements from your list as alternating matching groups. (re.split keeps groups when splitting.)



          >>> import re
          >>> lst = ['Na', 'K']
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> regex
          >>> '(Na)|(K)'


          Apply the regex...



          >>> re.split(regex, 'NaCO3')
          >>> ['', 'Na', None, 'CO3']
          >>> re.split(regex, 'KCO3')
          >>> ['', None, 'K', 'CO3']


          ... and filter out falsy values (None, '')



          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['Na', 'CO3']
          >>> list(filter(None, re.split(regex, 'KCO3')))
          >>> ['K', 'CO3']


          You can assign to those values with extended iterable unpacking:



          >>> b1, b2, *unexpected_rest = filter(None, re.split(regex, 'KCO3'))
          >>> b1
          >>> 'K'
          >>> b2
          >>> 'CO3'


          If you want to bias the split in favor of longer matches, sort lst in descending order first.



          Not good:



          >>> lst = ['N', 'Na', 'CO3']
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['N', 'a', 'CO3']


          Better:



          >>> lst = ['N', 'Na', 'CO3']
          >>> lst = sorted(lst, key=len, reverse=True)
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['Na', 'CO3']


          Let me know if that works for you.






          share|improve this answer




















          • 1





            One potential problem is that most single letter element symbols can be mistakenly matched when there's a different element with a symbol that starts with the same letter. For example Nitrogen (N) / Sodium (Na) or Oxygen (O) and Osmium (Os). Maybe use a negative lookahead (Na|O|N)(?![a-z]) works?

            – Håken Lid
            Nov 11 '18 at 16:27












          • Man this seams very good, but just one question, what exactly the re function does? And the above guy is right, there is a way to differentiate them? (Basically that was the reason I choose to do a list comparison where all the variables follows the 'Chemistry Grammar').

            – Locochoco
            Nov 11 '18 at 16:31







          • 1





            re is the python standard library regular expressions module. docs.python.org/3.7/library/re.html

            – Håken Lid
            Nov 11 '18 at 16:33






          • 1





            I now that the bigger the compound the crazier the code must be, but for now I'm just starting to see what I can accomplish with stuff as simple as NaOH or KOH and your code was perfect to this b'cause I can even do this to the rest of the compound and make it (even in its simplicity) more "chemistry correct".

            – Locochoco
            Nov 11 '18 at 17:04






          • 1





            @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)

            – timgeb
            Nov 11 '18 at 17:05















          2














          Disclaimer



          Just to be clear: for the constrained scope of what you are doing this solution might be applicable. If you want to parse any chemical compound (and those can look quite complicated) you need a full fledged parser, not the toy regex solution I came up with.




          Here's an idea:



          Dynamically build a regex with elements from your list as alternating matching groups. (re.split keeps groups when splitting.)



          >>> import re
          >>> lst = ['Na', 'K']
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> regex
          >>> '(Na)|(K)'


          Apply the regex...



          >>> re.split(regex, 'NaCO3')
          >>> ['', 'Na', None, 'CO3']
          >>> re.split(regex, 'KCO3')
          >>> ['', None, 'K', 'CO3']


          ... and filter out falsy values (None, '')



          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['Na', 'CO3']
          >>> list(filter(None, re.split(regex, 'KCO3')))
          >>> ['K', 'CO3']


          You can assign to those values with extended iterable unpacking:



          >>> b1, b2, *unexpected_rest = filter(None, re.split(regex, 'KCO3'))
          >>> b1
          >>> 'K'
          >>> b2
          >>> 'CO3'


          If you want to bias the split in favor of longer matches, sort lst in descending order first.



          Not good:



          >>> lst = ['N', 'Na', 'CO3']
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['N', 'a', 'CO3']


          Better:



          >>> lst = ['N', 'Na', 'CO3']
          >>> lst = sorted(lst, key=len, reverse=True)
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['Na', 'CO3']


          Let me know if that works for you.






          share|improve this answer




















          • 1





            One potential problem is that most single letter element symbols can be mistakenly matched when there's a different element with a symbol that starts with the same letter. For example Nitrogen (N) / Sodium (Na) or Oxygen (O) and Osmium (Os). Maybe use a negative lookahead (Na|O|N)(?![a-z]) works?

            – Håken Lid
            Nov 11 '18 at 16:27












          • Man this seams very good, but just one question, what exactly the re function does? And the above guy is right, there is a way to differentiate them? (Basically that was the reason I choose to do a list comparison where all the variables follows the 'Chemistry Grammar').

            – Locochoco
            Nov 11 '18 at 16:31







          • 1





            re is the python standard library regular expressions module. docs.python.org/3.7/library/re.html

            – Håken Lid
            Nov 11 '18 at 16:33






          • 1





            I now that the bigger the compound the crazier the code must be, but for now I'm just starting to see what I can accomplish with stuff as simple as NaOH or KOH and your code was perfect to this b'cause I can even do this to the rest of the compound and make it (even in its simplicity) more "chemistry correct".

            – Locochoco
            Nov 11 '18 at 17:04






          • 1





            @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)

            – timgeb
            Nov 11 '18 at 17:05













          2












          2








          2







          Disclaimer



          Just to be clear: for the constrained scope of what you are doing this solution might be applicable. If you want to parse any chemical compound (and those can look quite complicated) you need a full fledged parser, not the toy regex solution I came up with.




          Here's an idea:



          Dynamically build a regex with elements from your list as alternating matching groups. (re.split keeps groups when splitting.)



          >>> import re
          >>> lst = ['Na', 'K']
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> regex
          >>> '(Na)|(K)'


          Apply the regex...



          >>> re.split(regex, 'NaCO3')
          >>> ['', 'Na', None, 'CO3']
          >>> re.split(regex, 'KCO3')
          >>> ['', None, 'K', 'CO3']


          ... and filter out falsy values (None, '')



          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['Na', 'CO3']
          >>> list(filter(None, re.split(regex, 'KCO3')))
          >>> ['K', 'CO3']


          You can assign to those values with extended iterable unpacking:



          >>> b1, b2, *unexpected_rest = filter(None, re.split(regex, 'KCO3'))
          >>> b1
          >>> 'K'
          >>> b2
          >>> 'CO3'


          If you want to bias the split in favor of longer matches, sort lst in descending order first.



          Not good:



          >>> lst = ['N', 'Na', 'CO3']
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['N', 'a', 'CO3']


          Better:



          >>> lst = ['N', 'Na', 'CO3']
          >>> lst = sorted(lst, key=len, reverse=True)
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['Na', 'CO3']


          Let me know if that works for you.






          share|improve this answer















          Disclaimer



          Just to be clear: for the constrained scope of what you are doing this solution might be applicable. If you want to parse any chemical compound (and those can look quite complicated) you need a full fledged parser, not the toy regex solution I came up with.




          Here's an idea:



          Dynamically build a regex with elements from your list as alternating matching groups. (re.split keeps groups when splitting.)



          >>> import re
          >>> lst = ['Na', 'K']
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> regex
          >>> '(Na)|(K)'


          Apply the regex...



          >>> re.split(regex, 'NaCO3')
          >>> ['', 'Na', None, 'CO3']
          >>> re.split(regex, 'KCO3')
          >>> ['', None, 'K', 'CO3']


          ... and filter out falsy values (None, '')



          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['Na', 'CO3']
          >>> list(filter(None, re.split(regex, 'KCO3')))
          >>> ['K', 'CO3']


          You can assign to those values with extended iterable unpacking:



          >>> b1, b2, *unexpected_rest = filter(None, re.split(regex, 'KCO3'))
          >>> b1
          >>> 'K'
          >>> b2
          >>> 'CO3'


          If you want to bias the split in favor of longer matches, sort lst in descending order first.



          Not good:



          >>> lst = ['N', 'Na', 'CO3']
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['N', 'a', 'CO3']


          Better:



          >>> lst = ['N', 'Na', 'CO3']
          >>> lst = sorted(lst, key=len, reverse=True)
          >>> regex = '|'.join('()'.format(a) for a in lst)
          >>> list(filter(None, re.split(regex, 'NaCO3')))
          >>> ['Na', 'CO3']


          Let me know if that works for you.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 '18 at 17:01

























          answered Nov 11 '18 at 16:19









          timgebtimgeb

          50.7k116393




          50.7k116393







          • 1





            One potential problem is that most single letter element symbols can be mistakenly matched when there's a different element with a symbol that starts with the same letter. For example Nitrogen (N) / Sodium (Na) or Oxygen (O) and Osmium (Os). Maybe use a negative lookahead (Na|O|N)(?![a-z]) works?

            – Håken Lid
            Nov 11 '18 at 16:27












          • Man this seams very good, but just one question, what exactly the re function does? And the above guy is right, there is a way to differentiate them? (Basically that was the reason I choose to do a list comparison where all the variables follows the 'Chemistry Grammar').

            – Locochoco
            Nov 11 '18 at 16:31







          • 1





            re is the python standard library regular expressions module. docs.python.org/3.7/library/re.html

            – Håken Lid
            Nov 11 '18 at 16:33






          • 1





            I now that the bigger the compound the crazier the code must be, but for now I'm just starting to see what I can accomplish with stuff as simple as NaOH or KOH and your code was perfect to this b'cause I can even do this to the rest of the compound and make it (even in its simplicity) more "chemistry correct".

            – Locochoco
            Nov 11 '18 at 17:04






          • 1





            @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)

            – timgeb
            Nov 11 '18 at 17:05












          • 1





            One potential problem is that most single letter element symbols can be mistakenly matched when there's a different element with a symbol that starts with the same letter. For example Nitrogen (N) / Sodium (Na) or Oxygen (O) and Osmium (Os). Maybe use a negative lookahead (Na|O|N)(?![a-z]) works?

            – Håken Lid
            Nov 11 '18 at 16:27












          • Man this seams very good, but just one question, what exactly the re function does? And the above guy is right, there is a way to differentiate them? (Basically that was the reason I choose to do a list comparison where all the variables follows the 'Chemistry Grammar').

            – Locochoco
            Nov 11 '18 at 16:31







          • 1





            re is the python standard library regular expressions module. docs.python.org/3.7/library/re.html

            – Håken Lid
            Nov 11 '18 at 16:33






          • 1





            I now that the bigger the compound the crazier the code must be, but for now I'm just starting to see what I can accomplish with stuff as simple as NaOH or KOH and your code was perfect to this b'cause I can even do this to the rest of the compound and make it (even in its simplicity) more "chemistry correct".

            – Locochoco
            Nov 11 '18 at 17:04






          • 1





            @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)

            – timgeb
            Nov 11 '18 at 17:05







          1




          1





          One potential problem is that most single letter element symbols can be mistakenly matched when there's a different element with a symbol that starts with the same letter. For example Nitrogen (N) / Sodium (Na) or Oxygen (O) and Osmium (Os). Maybe use a negative lookahead (Na|O|N)(?![a-z]) works?

          – Håken Lid
          Nov 11 '18 at 16:27






          One potential problem is that most single letter element symbols can be mistakenly matched when there's a different element with a symbol that starts with the same letter. For example Nitrogen (N) / Sodium (Na) or Oxygen (O) and Osmium (Os). Maybe use a negative lookahead (Na|O|N)(?![a-z]) works?

          – Håken Lid
          Nov 11 '18 at 16:27














          Man this seams very good, but just one question, what exactly the re function does? And the above guy is right, there is a way to differentiate them? (Basically that was the reason I choose to do a list comparison where all the variables follows the 'Chemistry Grammar').

          – Locochoco
          Nov 11 '18 at 16:31






          Man this seams very good, but just one question, what exactly the re function does? And the above guy is right, there is a way to differentiate them? (Basically that was the reason I choose to do a list comparison where all the variables follows the 'Chemistry Grammar').

          – Locochoco
          Nov 11 '18 at 16:31





          1




          1





          re is the python standard library regular expressions module. docs.python.org/3.7/library/re.html

          – Håken Lid
          Nov 11 '18 at 16:33





          re is the python standard library regular expressions module. docs.python.org/3.7/library/re.html

          – Håken Lid
          Nov 11 '18 at 16:33




          1




          1





          I now that the bigger the compound the crazier the code must be, but for now I'm just starting to see what I can accomplish with stuff as simple as NaOH or KOH and your code was perfect to this b'cause I can even do this to the rest of the compound and make it (even in its simplicity) more "chemistry correct".

          – Locochoco
          Nov 11 '18 at 17:04





          I now that the bigger the compound the crazier the code must be, but for now I'm just starting to see what I can accomplish with stuff as simple as NaOH or KOH and your code was perfect to this b'cause I can even do this to the rest of the compound and make it (even in its simplicity) more "chemistry correct".

          – Locochoco
          Nov 11 '18 at 17:04




          1




          1





          @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)

          – timgeb
          Nov 11 '18 at 17:05





          @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)

          – timgeb
          Nov 11 '18 at 17:05

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250525%2fseparating-strings-with-lists%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

          How do I collapse sections of code in Visual Studio Code for Windows?