Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7652283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:43:41+00:00 2026-05-31T11:43:41+00:00

After inputting a string such as: ‘3,11,15,16,35’ if you wanted each number to represent

  • 0

After inputting a string such as: '3,11,15,16,35'
if you wanted each number to represent the line-number of some code,
for the purpose of adding a comment to those lines,
what would you do?

More specifically, in a for-loop where you iterate over each line of code, how would you check the string to see if it contains the current line number.
Here’s there relevant section of what I tried:

self.num = input('Line(s) to number?')
self.linelist = self.code.splitlines()

for i, element in enumerate(self.linelist):
    self.count += 1
    # if match(str(self.count) + r",", self.num):
    if self.num.find(str(self.count) + ','):
        self.final = self.final + element + ' # line ' + str(self.count) + '\n'
    else:
        self.final = self.final + element + '\n'

The re.match attempt only comments the first line-number in the string.
The find attempt seems to match the first,
but comments everything other than the line associated with that number.
The other issue with this setup is that 1, could be found if 11, was in the list.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:43:42+00:00Added an answer on May 31, 2026 at 11:43 am

    Problem is, you are using the result of find directly in an if statement. Just look at what find returns:

    Return the lowest index in s where the substring sub is found such
    that sub is wholly contained in s[start:end]. Return -1 on failure.
    Defaults for start and end and interpretation of negative values is
    the same as for slices.

    So, you will be getting an integer corresponding the index of first match or -1. When you do if an_integer:, it is actually doing if bool(an_integer):. bool(an_integer) is False for an_integer==0 and True for everything else. That means you’ll be doing else part if your line number is found at the beginning of the input and if part for everything else. You’ll need to do something like:

    if self.num.find(str(self.count) + ',') >= 0:
    

    to denote a match.

    As for the re.match part, re.match tries to match a substring from the beginning of the string. You should use re.search instead.

    That being said, even with these fixes and even with the delimiter you’ll still have the problem of mismatch as you identified. 11, will match both 1, and 11,. To solve this, you can split the input with delimiter and obtain a list of values. Then you can check if the value is in that list:

    self.num = input('Line(s) to number?').split(",")
    # ...
        if str(self.count) in self.num:
        #...
    

    As a small side note, you are already using enumerate to get the line numbers. That should eliminate the use of counter (i.e. self.count). If you want them to start from 1, you can tell enumerate to do so by giving the optional second argument:

    for i, element in enumerate(self.list, 1):
    

    Then, use i instead of self.count.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading the Head First Design Patterns book and using a number of other
I originally had this code written as a series of if statements, but after
I have the following code that works correctly. However after I add an else
I have a script which allows me to lookup for a hostname after inputting
hello i have following code and it works fine for finding and replacing string...
I want to populate a listbox after inputting text into a text box and
I modified the code here (http://www.w3schools.com/PHP/php_ajax_database.asp) to update and display my database after an
Hey I came up with a small bit of php code, after looking at
After the suggestion to use a library for my ajax needs I am going
After reading this question , I was reminded of when I was taught Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.