簡體   English   中英

無法在Python中用re.sub進行就地替換

[英]Unable to make a in place substitution with re.sub in Python

我有一個列表,列表中的每個元素都是一章的標題。 每個標題的格式如下:'[系列名稱] [章節編號]:[章節標題]'因此,我的清單的摘錄是

chapter_title:['One Piece 1 : Romance Dawn', 'One Piece 2 : They Call Him Strawhat Luffy', 'One Piece 3 : Pirate Hunter Zoro Enters']

我想刪除章節號和冒號之間的空格。 我的工作代碼是:

no_space_regex = re.compile(r'\s:')
for i in chapter_title:
    no_space_regex.sub(':',i)

但是,它並沒有替代。 而且,我知道編譯的工作原理,因為如果我使用re.findall,它將找到所有空白,后跟冒號。

我有點解決了,使用:

no_space_regex = re.compile(r'\s:')
def_chapter=[] #list of chapter titles with no space before :
for i in chapter_title:
    i = no_space_regex.sub(':',i)
    def_chapter.append(i)

但我想知道為什么re.sub不能按原計划替代它。

re.sub無法更改字符串,因為字符串是不可變的。 它所能做的就是返回一個新字符串。

您的選擇是a)像以前一樣建立一個新列表,或b)如果出於某種原因您確實需要保留chapter_title的身份,則將其分配給舊列表的完整部分。

>>> import re
>>> 
>>> chapter_title = ['One Piece 1 : Romance Dawn', 'One Piece 2 : They Call Him Strawhat Luffy', 'One Piece 3 : Pirate Hunter Zoro Enters']
>>> no_space_regex = re.compile(r'\s:')
>>> 
>>> id(chapter_title)
139706643715336
>>> chapter_title[:] = (no_space_regex.sub(':', s) for s in chapter_title)
>>> chapter_title
['One Piece 1: Romance Dawn', 'One Piece 2: They Call Him Strawhat Luffy', 'One Piece 3: Pirate Hunter Zoro Enters']
>>> id(chapter_title)
139706643715336

請注意,第二種方法在另外修改chapter_title同時,仍會構建新的字符串。 在幾乎所有情況下,我能想到你原來的方法會工作得很好,並重新分配oneliner chapter_title是這樣的:

chapter_title = [no_space_regex.sub(':', s) for s in chapter_title]

編輯:將分配給全片的分配更改為右側的生成器表達式,以提高內存效率

暫無
暫無

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

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