Replacing letters using a dictionary









up vote
0
down vote

favorite












I had a text that I separated into individual sounds by means of graphemes. These graphemes are now part of a list, as can be seen below:



graphemes = ["th", "e", "g", "i", "r", "l", "th", "a", "t", "r", "ea", "d", "s", ...]


In addition to that, I also have a dictionary which links some of those graphemes to a number:



graph_nums = "th":1, "s":2, "t":3, ...


Finally, I have a set of conditions. For example, "if -s comes after a vowel" or "if -t comes before a consonant".



What I want to do is iterate over the list of graphemes and, if one of the conditions is met, then replace the grapheme with its corresponding number.



This is what I tried to do so far:



special_graphemes = ["s", "t"...] #a list with the characters that are mentioned in the conditions
vowels = ["a", "e", "i", "o", "u", ...] #a list with all the vowels and dipthongs
consonants = ["b", "c", "d", ...] #a list of all consonants and groups of consonants
output = ""

for grapheme in graphemes: #iterate over each grapheme
if grapheme in special_graphemes: #if the grapheme is one of the graphemes that needs to be replaced by a number
if graphemes[grapheme-1] in vowels: #for a condition like "if -s comes after a vowel", it needs to be checked whether the previous grapheme is a vowel
output += graph_nums.get(num) #if the previous condition applies, then replace the grapheme by its number, according to the dictionary
elif XXXX #other conditions checked in a similar way
else:
output += grapheme #otherwise, just keep the grapheme as it is
print(output)


However, when I run this, I get an error concerning the indexes (i.e., this is wrong: graphemes[grapheme-1]). How could I then access the positions I'm interested in and replace them when necessary?



Also, I'm not sure either whether the way I'm accessing the dictionary and replacing the graphemes is correct either.










share|improve this question

















  • 2




    Use for i, grapheme in enumerate(graphemes)
    – SilverSlash
    Nov 12 at 2:01










  • @SilverSlash where should I use that? Could you please expand?
    – Me All
    Nov 12 at 2:02










  • Have you solved your issue? If so you should add/mark a correct answer.
    – Shayn
    Nov 18 at 22:56














up vote
0
down vote

favorite












I had a text that I separated into individual sounds by means of graphemes. These graphemes are now part of a list, as can be seen below:



graphemes = ["th", "e", "g", "i", "r", "l", "th", "a", "t", "r", "ea", "d", "s", ...]


In addition to that, I also have a dictionary which links some of those graphemes to a number:



graph_nums = "th":1, "s":2, "t":3, ...


Finally, I have a set of conditions. For example, "if -s comes after a vowel" or "if -t comes before a consonant".



What I want to do is iterate over the list of graphemes and, if one of the conditions is met, then replace the grapheme with its corresponding number.



This is what I tried to do so far:



special_graphemes = ["s", "t"...] #a list with the characters that are mentioned in the conditions
vowels = ["a", "e", "i", "o", "u", ...] #a list with all the vowels and dipthongs
consonants = ["b", "c", "d", ...] #a list of all consonants and groups of consonants
output = ""

for grapheme in graphemes: #iterate over each grapheme
if grapheme in special_graphemes: #if the grapheme is one of the graphemes that needs to be replaced by a number
if graphemes[grapheme-1] in vowels: #for a condition like "if -s comes after a vowel", it needs to be checked whether the previous grapheme is a vowel
output += graph_nums.get(num) #if the previous condition applies, then replace the grapheme by its number, according to the dictionary
elif XXXX #other conditions checked in a similar way
else:
output += grapheme #otherwise, just keep the grapheme as it is
print(output)


However, when I run this, I get an error concerning the indexes (i.e., this is wrong: graphemes[grapheme-1]). How could I then access the positions I'm interested in and replace them when necessary?



Also, I'm not sure either whether the way I'm accessing the dictionary and replacing the graphemes is correct either.










share|improve this question

















  • 2




    Use for i, grapheme in enumerate(graphemes)
    – SilverSlash
    Nov 12 at 2:01










  • @SilverSlash where should I use that? Could you please expand?
    – Me All
    Nov 12 at 2:02










  • Have you solved your issue? If so you should add/mark a correct answer.
    – Shayn
    Nov 18 at 22:56












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I had a text that I separated into individual sounds by means of graphemes. These graphemes are now part of a list, as can be seen below:



graphemes = ["th", "e", "g", "i", "r", "l", "th", "a", "t", "r", "ea", "d", "s", ...]


In addition to that, I also have a dictionary which links some of those graphemes to a number:



graph_nums = "th":1, "s":2, "t":3, ...


Finally, I have a set of conditions. For example, "if -s comes after a vowel" or "if -t comes before a consonant".



What I want to do is iterate over the list of graphemes and, if one of the conditions is met, then replace the grapheme with its corresponding number.



This is what I tried to do so far:



special_graphemes = ["s", "t"...] #a list with the characters that are mentioned in the conditions
vowels = ["a", "e", "i", "o", "u", ...] #a list with all the vowels and dipthongs
consonants = ["b", "c", "d", ...] #a list of all consonants and groups of consonants
output = ""

for grapheme in graphemes: #iterate over each grapheme
if grapheme in special_graphemes: #if the grapheme is one of the graphemes that needs to be replaced by a number
if graphemes[grapheme-1] in vowels: #for a condition like "if -s comes after a vowel", it needs to be checked whether the previous grapheme is a vowel
output += graph_nums.get(num) #if the previous condition applies, then replace the grapheme by its number, according to the dictionary
elif XXXX #other conditions checked in a similar way
else:
output += grapheme #otherwise, just keep the grapheme as it is
print(output)


However, when I run this, I get an error concerning the indexes (i.e., this is wrong: graphemes[grapheme-1]). How could I then access the positions I'm interested in and replace them when necessary?



Also, I'm not sure either whether the way I'm accessing the dictionary and replacing the graphemes is correct either.










share|improve this question













I had a text that I separated into individual sounds by means of graphemes. These graphemes are now part of a list, as can be seen below:



graphemes = ["th", "e", "g", "i", "r", "l", "th", "a", "t", "r", "ea", "d", "s", ...]


In addition to that, I also have a dictionary which links some of those graphemes to a number:



graph_nums = "th":1, "s":2, "t":3, ...


Finally, I have a set of conditions. For example, "if -s comes after a vowel" or "if -t comes before a consonant".



What I want to do is iterate over the list of graphemes and, if one of the conditions is met, then replace the grapheme with its corresponding number.



This is what I tried to do so far:



special_graphemes = ["s", "t"...] #a list with the characters that are mentioned in the conditions
vowels = ["a", "e", "i", "o", "u", ...] #a list with all the vowels and dipthongs
consonants = ["b", "c", "d", ...] #a list of all consonants and groups of consonants
output = ""

for grapheme in graphemes: #iterate over each grapheme
if grapheme in special_graphemes: #if the grapheme is one of the graphemes that needs to be replaced by a number
if graphemes[grapheme-1] in vowels: #for a condition like "if -s comes after a vowel", it needs to be checked whether the previous grapheme is a vowel
output += graph_nums.get(num) #if the previous condition applies, then replace the grapheme by its number, according to the dictionary
elif XXXX #other conditions checked in a similar way
else:
output += grapheme #otherwise, just keep the grapheme as it is
print(output)


However, when I run this, I get an error concerning the indexes (i.e., this is wrong: graphemes[grapheme-1]). How could I then access the positions I'm interested in and replace them when necessary?



Also, I'm not sure either whether the way I'm accessing the dictionary and replacing the graphemes is correct either.







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 1:53









Me All

1207




1207







  • 2




    Use for i, grapheme in enumerate(graphemes)
    – SilverSlash
    Nov 12 at 2:01










  • @SilverSlash where should I use that? Could you please expand?
    – Me All
    Nov 12 at 2:02










  • Have you solved your issue? If so you should add/mark a correct answer.
    – Shayn
    Nov 18 at 22:56












  • 2




    Use for i, grapheme in enumerate(graphemes)
    – SilverSlash
    Nov 12 at 2:01










  • @SilverSlash where should I use that? Could you please expand?
    – Me All
    Nov 12 at 2:02










  • Have you solved your issue? If so you should add/mark a correct answer.
    – Shayn
    Nov 18 at 22:56







2




2




Use for i, grapheme in enumerate(graphemes)
– SilverSlash
Nov 12 at 2:01




Use for i, grapheme in enumerate(graphemes)
– SilverSlash
Nov 12 at 2:01












@SilverSlash where should I use that? Could you please expand?
– Me All
Nov 12 at 2:02




@SilverSlash where should I use that? Could you please expand?
– Me All
Nov 12 at 2:02












Have you solved your issue? If so you should add/mark a correct answer.
– Shayn
Nov 18 at 22:56




Have you solved your issue? If so you should add/mark a correct answer.
– Shayn
Nov 18 at 22:56












2 Answers
2






active

oldest

votes

















up vote
0
down vote













A loop like for e in a means that every e is an element of a, if you want the indexes you'll have to use the builtin enumerate. Your code will then become:



for i, grapheme in enumerate (graphemes): # Here i is the index and grapheme is the element at graphemes[i]
if grapheme in special_graphemes:
if i > 0 and graphemes[i-1] in vowels: # Now you can use i-1 to check the previous element, watch out for when i == 0
output += graph_nums.get(num) # I assume this (and the rest) functions as requires
elif XXXX
else:
output += grapheme





share|improve this answer



























    up vote
    0
    down vote













    Using Python list comprehension



    Try this :



    graphemes = ["th", "e", "g", "i", "r", "l", "th", "a", "t", "r", "ea", "d", "s"]
    graph_nums = "th":1, "s":2, "t":3
    out_graphemes = [ x for x in (map(graph_nums.get, graphemes, graphemes)) ]
    print (out_graphemes)


    Output



    [1, 'e', 'g', 'i', 'r', 'l', 1, 'a', 3, 'r', 'ea', 'd', 2]






    share|improve this answer






















      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%2f53255085%2freplacing-letters-using-a-dictionary%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      A loop like for e in a means that every e is an element of a, if you want the indexes you'll have to use the builtin enumerate. Your code will then become:



      for i, grapheme in enumerate (graphemes): # Here i is the index and grapheme is the element at graphemes[i]
      if grapheme in special_graphemes:
      if i > 0 and graphemes[i-1] in vowels: # Now you can use i-1 to check the previous element, watch out for when i == 0
      output += graph_nums.get(num) # I assume this (and the rest) functions as requires
      elif XXXX
      else:
      output += grapheme





      share|improve this answer
























        up vote
        0
        down vote













        A loop like for e in a means that every e is an element of a, if you want the indexes you'll have to use the builtin enumerate. Your code will then become:



        for i, grapheme in enumerate (graphemes): # Here i is the index and grapheme is the element at graphemes[i]
        if grapheme in special_graphemes:
        if i > 0 and graphemes[i-1] in vowels: # Now you can use i-1 to check the previous element, watch out for when i == 0
        output += graph_nums.get(num) # I assume this (and the rest) functions as requires
        elif XXXX
        else:
        output += grapheme





        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          A loop like for e in a means that every e is an element of a, if you want the indexes you'll have to use the builtin enumerate. Your code will then become:



          for i, grapheme in enumerate (graphemes): # Here i is the index and grapheme is the element at graphemes[i]
          if grapheme in special_graphemes:
          if i > 0 and graphemes[i-1] in vowels: # Now you can use i-1 to check the previous element, watch out for when i == 0
          output += graph_nums.get(num) # I assume this (and the rest) functions as requires
          elif XXXX
          else:
          output += grapheme





          share|improve this answer












          A loop like for e in a means that every e is an element of a, if you want the indexes you'll have to use the builtin enumerate. Your code will then become:



          for i, grapheme in enumerate (graphemes): # Here i is the index and grapheme is the element at graphemes[i]
          if grapheme in special_graphemes:
          if i > 0 and graphemes[i-1] in vowels: # Now you can use i-1 to check the previous element, watch out for when i == 0
          output += graph_nums.get(num) # I assume this (and the rest) functions as requires
          elif XXXX
          else:
          output += grapheme






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 2:12









          Shayn

          559318




          559318






















              up vote
              0
              down vote













              Using Python list comprehension



              Try this :



              graphemes = ["th", "e", "g", "i", "r", "l", "th", "a", "t", "r", "ea", "d", "s"]
              graph_nums = "th":1, "s":2, "t":3
              out_graphemes = [ x for x in (map(graph_nums.get, graphemes, graphemes)) ]
              print (out_graphemes)


              Output



              [1, 'e', 'g', 'i', 'r', 'l', 1, 'a', 3, 'r', 'ea', 'd', 2]






              share|improve this answer


























                up vote
                0
                down vote













                Using Python list comprehension



                Try this :



                graphemes = ["th", "e", "g", "i", "r", "l", "th", "a", "t", "r", "ea", "d", "s"]
                graph_nums = "th":1, "s":2, "t":3
                out_graphemes = [ x for x in (map(graph_nums.get, graphemes, graphemes)) ]
                print (out_graphemes)


                Output



                [1, 'e', 'g', 'i', 'r', 'l', 1, 'a', 3, 'r', 'ea', 'd', 2]






                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Using Python list comprehension



                  Try this :



                  graphemes = ["th", "e", "g", "i", "r", "l", "th", "a", "t", "r", "ea", "d", "s"]
                  graph_nums = "th":1, "s":2, "t":3
                  out_graphemes = [ x for x in (map(graph_nums.get, graphemes, graphemes)) ]
                  print (out_graphemes)


                  Output



                  [1, 'e', 'g', 'i', 'r', 'l', 1, 'a', 3, 'r', 'ea', 'd', 2]






                  share|improve this answer














                  Using Python list comprehension



                  Try this :



                  graphemes = ["th", "e", "g", "i", "r", "l", "th", "a", "t", "r", "ea", "d", "s"]
                  graph_nums = "th":1, "s":2, "t":3
                  out_graphemes = [ x for x in (map(graph_nums.get, graphemes, graphemes)) ]
                  print (out_graphemes)


                  Output



                  [1, 'e', 'g', 'i', 'r', 'l', 1, 'a', 3, 'r', 'ea', 'd', 2]







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 12 at 2:49

























                  answered Nov 12 at 2:40









                  Arijit Ghosh

                  398




                  398



























                      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%2f53255085%2freplacing-letters-using-a-dictionary%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