简体   繁体   English

删除列表列表列表中“”中的空格

[英]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"']]]

I tried with following, but this seems to get rid of middle word in " " in python3.我尝试跟随,但这似乎摆脱了python3中“”中的中间词。

print([re.sub(r'"(\w+)(\s(\w+))*"', '"\\1\\3"', x.lower()) for x in results[0]])

My desired output is我想要的输出是

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"']]]

Remove only between "" and lowercase in "" so that "John P Krul" to "johnpkrul" while keeping everything same.仅在“”和“”中的小写字母之间删除,以便“John P Krul”变为“johnpkrul”,同时保持所有内容相同。

What needs to be changed to code?代码需要改什么?

You can try this.你可以试试这个。

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"']

Edit: For list of list of lists编辑:对于列表列表

[[[re.sub(r'\"([^"]*)\"',f,i) for i in lst2] for lst2 in lst1] for lst1 in results]

Output:输出:

[[['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.

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