[英]Remove spaces in " " in List of list of list
results = [[['2020 is the year', '29 year old "Samuel G"', '25 year old "John P Krul"', '40 year old "Trey Nunez S"', '22 year old "Fiona S Paul"', '50 year old "Sean J Beal"']]]
我嘗試跟隨,但這似乎擺脫了python3中“”中的中間詞。
print([re.sub(r'"(\w+)(\s(\w+))*"', '"\\1\\3"', x.lower()) for x in results[0]])
我想要的輸出是
results = [[['2020 is the year', '29 year old "samuelg"', '25 year old "johnpkrul"', '40 year old "treynunezs"', '22 year old "fionaspaul"', '50 year old "seanjbeal"']]]
僅在“”和“”中的小寫字母之間刪除,以便“John P Krul”變為“johnpkrul”,同時保持所有內容相同。
代碼需要改什么?
你可以試試這個。
def f(x): #Takes re.match object as input
a=x.group() #extractting the match
return a.replace(' ','').lower() #them to lower and removing spaces
[re.sub(r'\"([^"]*)\"',f,i) for i in results]
['2020 is the year',
'29 year old "samuelg"',
'25 year old "johnpkrul"',
'40 year old "treynunezs"',
'22 year old "fionaspaul"',
'50 year old "seanjbeal"']
編輯:對於列表列表
[[[re.sub(r'\"([^"]*)\"',f,i) for i in lst2] for lst2 in lst1] for lst1 in results]
輸出:
[[['2020 is the year',
'29 year old "samuelg"',
'25 year old "johnpkrul"',
'40 year old "treynunezs"',
'22 year old "fionaspaul"',
'50 year old "seanjbeal"']]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.