find a substring in a selected portion of a list elements





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have the below string list (list1) and I want to find if str b is present anywhere in left hand side portion of an element before the decimal in list1.



I tried the below code but it finds all the elements where str b is found.



list1= ['4.39', '5.25', '2.29', '3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16', '4.4']
b=str(1)
print([s for s in list1 if b in s])


it returns the following:



['3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16']


However, I want to get only 1.5 because this is the only element where string b matches the left hand side part before decimal. Remember the elements are in string format. Any fast way of checking this thing?










share|improve this question































    1















    I have the below string list (list1) and I want to find if str b is present anywhere in left hand side portion of an element before the decimal in list1.



    I tried the below code but it finds all the elements where str b is found.



    list1= ['4.39', '5.25', '2.29', '3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16', '4.4']
    b=str(1)
    print([s for s in list1 if b in s])


    it returns the following:



    ['3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16']


    However, I want to get only 1.5 because this is the only element where string b matches the left hand side part before decimal. Remember the elements are in string format. Any fast way of checking this thing?










    share|improve this question



























      1












      1








      1








      I have the below string list (list1) and I want to find if str b is present anywhere in left hand side portion of an element before the decimal in list1.



      I tried the below code but it finds all the elements where str b is found.



      list1= ['4.39', '5.25', '2.29', '3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16', '4.4']
      b=str(1)
      print([s for s in list1 if b in s])


      it returns the following:



      ['3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16']


      However, I want to get only 1.5 because this is the only element where string b matches the left hand side part before decimal. Remember the elements are in string format. Any fast way of checking this thing?










      share|improve this question
















      I have the below string list (list1) and I want to find if str b is present anywhere in left hand side portion of an element before the decimal in list1.



      I tried the below code but it finds all the elements where str b is found.



      list1= ['4.39', '5.25', '2.29', '3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16', '4.4']
      b=str(1)
      print([s for s in list1 if b in s])


      it returns the following:



      ['3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16']


      However, I want to get only 1.5 because this is the only element where string b matches the left hand side part before decimal. Remember the elements are in string format. Any fast way of checking this thing?







      python string list list-comprehension python-3.5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 15:37









      jpp

      103k2167117




      103k2167117










      asked Nov 22 '18 at 15:33









      HT121HT121

      1168




      1168
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You need to split each string by . and extract the first split:



          print([s for s in list1 if '1' in s.split('.')[0]])

          ['1.5']


          For a precise match, use ==:



          print([s for s in list1 if s.split('.')[0] == '1'])

          ['1.5']





          share|improve this answer


























          • And do you know how could i find the actual index of matched element in list1?

            – HT121
            Nov 22 '18 at 15:47






          • 1





            [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.

            – jpp
            Nov 22 '18 at 15:59













          • Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])

            – HT121
            Nov 22 '18 at 16:08











          • @HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).

            – jpp
            Nov 22 '18 at 16:10














          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%2f53434191%2ffind-a-substring-in-a-selected-portion-of-a-list-elements%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














          You need to split each string by . and extract the first split:



          print([s for s in list1 if '1' in s.split('.')[0]])

          ['1.5']


          For a precise match, use ==:



          print([s for s in list1 if s.split('.')[0] == '1'])

          ['1.5']





          share|improve this answer


























          • And do you know how could i find the actual index of matched element in list1?

            – HT121
            Nov 22 '18 at 15:47






          • 1





            [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.

            – jpp
            Nov 22 '18 at 15:59













          • Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])

            – HT121
            Nov 22 '18 at 16:08











          • @HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).

            – jpp
            Nov 22 '18 at 16:10


















          2














          You need to split each string by . and extract the first split:



          print([s for s in list1 if '1' in s.split('.')[0]])

          ['1.5']


          For a precise match, use ==:



          print([s for s in list1 if s.split('.')[0] == '1'])

          ['1.5']





          share|improve this answer


























          • And do you know how could i find the actual index of matched element in list1?

            – HT121
            Nov 22 '18 at 15:47






          • 1





            [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.

            – jpp
            Nov 22 '18 at 15:59













          • Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])

            – HT121
            Nov 22 '18 at 16:08











          • @HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).

            – jpp
            Nov 22 '18 at 16:10
















          2












          2








          2







          You need to split each string by . and extract the first split:



          print([s for s in list1 if '1' in s.split('.')[0]])

          ['1.5']


          For a precise match, use ==:



          print([s for s in list1 if s.split('.')[0] == '1'])

          ['1.5']





          share|improve this answer















          You need to split each string by . and extract the first split:



          print([s for s in list1 if '1' in s.split('.')[0]])

          ['1.5']


          For a precise match, use ==:



          print([s for s in list1 if s.split('.')[0] == '1'])

          ['1.5']






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 15:37

























          answered Nov 22 '18 at 15:35









          jppjpp

          103k2167117




          103k2167117













          • And do you know how could i find the actual index of matched element in list1?

            – HT121
            Nov 22 '18 at 15:47






          • 1





            [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.

            – jpp
            Nov 22 '18 at 15:59













          • Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])

            – HT121
            Nov 22 '18 at 16:08











          • @HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).

            – jpp
            Nov 22 '18 at 16:10





















          • And do you know how could i find the actual index of matched element in list1?

            – HT121
            Nov 22 '18 at 15:47






          • 1





            [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.

            – jpp
            Nov 22 '18 at 15:59













          • Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])

            – HT121
            Nov 22 '18 at 16:08











          • @HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).

            – jpp
            Nov 22 '18 at 16:10



















          And do you know how could i find the actual index of matched element in list1?

          – HT121
          Nov 22 '18 at 15:47





          And do you know how could i find the actual index of matched element in list1?

          – HT121
          Nov 22 '18 at 15:47




          1




          1





          [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.

          – jpp
          Nov 22 '18 at 15:59







          [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.

          – jpp
          Nov 22 '18 at 15:59















          Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])

          – HT121
          Nov 22 '18 at 16:08





          Thank you jpp. it works fine. Just a small comment, there seems to be a small typo in the code. There is one additional bracket by mistake after [0] in the split s.split('.')[0])

          – HT121
          Nov 22 '18 at 16:08













          @HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).

          – jpp
          Nov 22 '18 at 16:10







          @HT121, I think it should be fine, there are 2 square open brackets and 2 closing ones? I edited it to make a list comprehension earlier (you may wish to refresh).

          – jpp
          Nov 22 '18 at 16:10






















          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%2f53434191%2ffind-a-substring-in-a-selected-portion-of-a-list-elements%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 to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents