简体   繁体   English

您如何在任意行中添加注释?

[英]How would you add comments to an arbitrary amount of lines?

After inputting a string such as: '3,11,15,16,35' 输入一个字符串,例如: '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. 更具体地说,在一个for循环中,您遍历代码的每一行,如何检查字符串以查看它是否包含当前行号。 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. re.match尝试仅注释字符串中的第一个行号。
The find attempt seems to match the first, find尝试似乎与第一个匹配,
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. 此设置的另一个问题是,如果列表中有11,则可以找到1,

Problem is, you are using the result of find directly in an if statement. 问题是,您直接在if语句中使用find的结果。 Just look at what find returns: 只需查看find结果即可:

Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. 返回s中找到子字符串sub的最低索引,以使sub完全包含在s [start:end]中。 Return -1 on failure. 失败时返回-1。 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 . 因此,您将获得一个与first match或-1的索引相对应的integer When you do if an_integer: , it is actually doing if bool(an_integer): . 当您执行if an_integer: ,实际上是在执行if bool(an_integer): bool(an_integer) is False for an_integer==0 and True for everything else. 对于an_integer==0bool(an_integer)False ,其他所有条件为True 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. 这意味着你会做else ,如果你的行号在输入的开始,发现部分if其他一切的一部分。 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 . 至于re.match部分, re.match尝试从string的开头匹配一个子字符串 You should use re.search instead. 您应该改用re.search

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, . 11,将匹配1,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. 作为一个小注释,您已经在使用enumerate获取行号。 That should eliminate the use of counter (ie self.count ). 那应该self.count使用counter(即self.count )。 If you want them to start from 1 , you can tell enumerate to do so by giving the optional second argument: 如果希望它们从1开始,则可以通过提供可选的第二个参数来告知enumerate

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

Then, use i instead of self.count . 然后,使用i代替self.count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM