1

我有一个这样的列表:

list=[' is ', ' the ', ' . ', ' .kda ', ...]

我想知道如何改变它,如下所示:

1-放在|每个之后'

2-删除'除第一个和最后一个之外的所有内容

3-删除所有,

4-从列表的乞讨和结尾移除[]

5-改变所有.\.即使它并不孤单。例如更改u.su\.s

输出:

list= is | the | \. | \.kda | ...

4

3 回答 3

2

使用列表理解re.sub

import re
list = [' is ', ' the ', ' . ', ' .kda ']
s = '|'.join([re.sub(r'\.', '\.', x) for x in list])
print(s)
# is | the | \. | \.kda 
于 2021-01-19T20:07:57.837 回答
1

字符串操作

answer = str(list).replace("'", "'|").replace(".", "\.").strip('[').strip(']')
于 2021-01-19T20:02:54.043 回答
1
list=[' is ', ' the ', ' . ', ' .kda ']
# add a r before the string declaration to specify it as a regex string
re_list = r"|".join(list).replace('.', '\.')
print(re_list)

#>>>' is | the |  \. |  \.kda | ...'
于 2021-01-19T20:09:07.873 回答