假設我們已經讀取了一個帶有多行注釋的 python 檔案,然后是一些代碼。這存盤data為一個list或np.ndarray
data = ["# this", "# is" "# the first comment", "print('hello world')", "# second comment"]
expected_output = ["```this is the first comment```", "print('hello world')", "``` second comment```"]
expected_output
所需的輸出將以字符開頭的多個元素替換為包含在字符#中的單個決議注釋backtick
['```this is the first comment```',
"print('hello world')",
'``` second comment```']
我可以進行決議,但我不知道如何用新格式化的單行替換單行([0, 1, 2]例如上面示例中的索引)。
到目前為止的腳本:
from pathlib import Path
import numpy as np
from itertools import groupby
from operator import itemgetter
def get_consecutive_group_edges(data: np.ndarray):
# https://stackoverflow.com/a/2154437/9940782
edges = []
for k, g in groupby(enumerate(data),lambda x:x[0]-x[1]):
group = (map(itemgetter(1),g))
group = list(map(int, group))
edges.append((group[0],group[-1]))
# convert ranges into group index
# https://stackoverflow.com/a/952952/9940782
group_lookup = dict(enumerate(edges))
return group_lookup
if __name__ == "__main__":
# https://stackoverflow.com/a/17141572/9940782
filedata = ["# this", "# is" "# the first comment", "print('hello world')", "# second comment"]
# find all consecutive lines starting as comments
comment_lines = np.argwhere([l[0] == "#" for l in filedata])
group_lookup = get_consecutive_group_edges(comment_lines)
output_lines = []
for comment_idx in group_lookup.keys():
# extract the comment groups
min_comment_line = group_lookup[comment_idx][0]
max_comment_line = group_lookup[comment_idx][1] 1
data = filedata[min_comment_line: max_comment_line]
# remove the comment characters
output = "".join(data).replace("\n", " ").replace("#", "")
# wrap in ```
output = "```" output "```" "\n"
我在最后一步失敗了:如何將每個之間的所有值替換為每個新決議的單個值?min_comment_linemax_comment_linegroupoutput
我可以對未注釋的行做些什么嗎?
non_comment_lines = np.argwhere([l[0] != "#" for l in filedata])
uj5u.com熱心網友回復:
您可以在 Python 中分配給串列切片,它可以用一個替換多個元素:
...
# make a copy of the original list, so we can replace the comments
output_lines = filedata.copy()
# iterate backwards so the indices line up
for comment_idx in reversed(group_lookup):
# extract the comment groups
min_comment_line = group_lookup[comment_idx][0]
max_comment_line = group_lookup[comment_idx][1] 1
data = filedata[min_comment_line:max_comment_line]
# remove the comment characters
output = "".join(data).replace("\n", " ").replace("#", "")
# wrap in ```
output = "```" output "```"
output_lines[min_comment_line:max_comment_line] = [output]
但是,整個操作可以簡單得多,因為groupby只對連續匹配的元素進行分組:
output_lines = []
# iterate over consecutive sections of comments and code
for is_comment, lines in groupby(filedata, key=lambda x: x[0] == "#"):
if is_comment:
# remove the comment characters
output = "".join(lines).replace("\n", " ").replace("#", "")
# wrap in ```
output_lines.append("```" output "```")
else:
# leave code lines unchanged
output_lines.extend(lines)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/436248.html
