Separating strings with lists









up vote
3
down vote

favorite












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



























    up vote
    3
    down vote

    favorite












    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

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      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 at 16:58

























      asked Nov 11 at 16:00









      Locochoco

      187




      187






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          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 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 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 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 at 17:04






          • 1




            @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)
            – timgeb
            Nov 11 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',
          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








          up vote
          2
          down vote



          accepted










          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 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 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 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 at 17:04






          • 1




            @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)
            – timgeb
            Nov 11 at 17:05














          up vote
          2
          down vote



          accepted










          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 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 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 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 at 17:04






          • 1




            @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)
            – timgeb
            Nov 11 at 17:05












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          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 at 17:01

























          answered Nov 11 at 16:19









          timgeb

          47.6k116288




          47.6k116288







          • 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 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 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 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 at 17:04






          • 1




            @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)
            – timgeb
            Nov 11 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 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 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 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 at 17:04






          • 1




            @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)
            – timgeb
            Nov 11 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 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 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 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 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 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 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 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 at 17:04




          1




          1




          @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)
          – timgeb
          Nov 11 at 17:05




          @Locochoco cool, glad I could help. It's nice to see fellow chemists on stackoverflow. :)
          – timgeb
          Nov 11 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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto