find a substring in a selected portion of a list elements











up vote
1
down vote

favorite












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




























    up vote
    1
    down vote

    favorite












    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


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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 at 15:37









      jpp

      87.8k195099




      87.8k195099










      asked Nov 22 at 15:33









      HT121

      817




      817
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          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 at 15:47






          • 1




            [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.
            – jpp
            Nov 22 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 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 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',
          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








          up vote
          2
          down vote



          accepted










          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 at 15:47






          • 1




            [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.
            – jpp
            Nov 22 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 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 at 16:10

















          up vote
          2
          down vote



          accepted










          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 at 15:47






          • 1




            [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.
            – jpp
            Nov 22 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 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 at 16:10















          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          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 at 15:37

























          answered Nov 22 at 15:35









          jpp

          87.8k195099




          87.8k195099












          • And do you know how could i find the actual index of matched element in list1?
            – HT121
            Nov 22 at 15:47






          • 1




            [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.
            – jpp
            Nov 22 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 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 at 16:10




















          • And do you know how could i find the actual index of matched element in list1?
            – HT121
            Nov 22 at 15:47






          • 1




            [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.
            – jpp
            Nov 22 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 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 at 16:10


















          And do you know how could i find the actual index of matched element in list1?
          – HT121
          Nov 22 at 15:47




          And do you know how could i find the actual index of matched element in list1?
          – HT121
          Nov 22 at 15:47




          1




          1




          [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.
          – jpp
          Nov 22 at 15:59






          [idx for idx, s in enumerate(list1) if '1' in s.split('.')[0]] should do it.
          – jpp
          Nov 22 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 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 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 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 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.





          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%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

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          How to ignore python UserWarning in pytest?

          Alexandru Averescu