簡體   English   中英

有沒有辦法替換和刪除多行字符串的行

[英]is there a way to replace and remove lines of a multi-lines string

我正在嘗試處理多行字符串,替換並刪除一些行。 這是代碼。

>>> txt
'1 Introduction\nPart I: Applied Math and Machine Learning Basics\n2 Linear Algebra'
>>> tmp = []
>>> for line in txt.splitlines():
...     if re.findall('[0-9]', line):
...         replaced = re.sub('[0-9]', '#', line)
...         tmp.append(replaced)
>>> print(tmp)
['# Introduction', '# Linear Algebra']

雖然這段代碼完成了我的工作,但我不確定它是否是最有效的方式。

我試過這篇文章文檔 ,看起來他們的多重發現都不是多行的。

有沒有更有效的方法來做到這一點?

您可以將列表理解用於問題中提供的代碼,這使得代碼整潔。

[re.sub('[0-9]', '#', line) for line in txt.splitlines() if re.findall('[0-9]', line) ]

# Output 
['# Introduction', '# Linear Algebra']

另外,就像@CertainPerformance在評論中提到的那樣,因為你只想知道字符串中是否存在數字,最好使用search而不是findall 然后你可以重新編寫列表理解代碼,

[re.sub('[0-9]', '#', line) for line in txt.splitlines() if re.search('[0-9]', line) ]

# Output 
['# Introduction', '# Linear Algebra']

在我的機器上使用search ,我可以看到一個小的性能提升。

%%timeit 1000000

[re.sub('[0-9]', '#', line) for line in txt.splitlines() if re.search('[0-9]', line) ]

# 4.76 µs ± 53.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%%timeit 1000000

[re.sub('[0-9]', '#', line) for line in txt.splitlines() if re.findall('[0-9]', line) ]

# 5.21 µs ± 114 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM