Python 将字符连接到列表
Python concatenate char to list
初始输入为:
input = [60, 20, 50, 70, …, 90] # ASCII characters
我想要这样的输出:
f_paths = ['/ev/path1', '/ev/path2']
通过连接到字符串将 ASCII 字符更改为文本。
paths = ''.join([chr(i) for i in input if chr(i) not in '<,'])
现在路径字符串如下所示:
paths=/notpath/exclude>/ev/path1>/ev/path2>
现在我想排除掉不需要的初始路径,保存剩下的路径
start = len(paths[0:paths.find(">")]) + 1
f_paths = []
g=''
for x in paths[start:]:
if x != '>':
g = g + x
else:
f_paths.append(g)
g = ''
输出是预期的,但必须有一种更优化的方法来执行 for 循环,问题是我不知道如何。
你可以这样做:
paths='/notpath/exclude>/ev/path1>/ev/path2>'
f_paths = paths.split('>')[1:-1]
输出:
['/ev/path1', '/ev/path2']
您可以使用 regex
:
import re
paths = '/notpath/exclude>/ev/path1>/ev/path2>'
print(re.findall(r'(?<=>).*?(?=>)', paths))
# ['/ev/path1', '/ev/path2']
初始输入为:
input = [60, 20, 50, 70, …, 90] # ASCII characters
我想要这样的输出:
f_paths = ['/ev/path1', '/ev/path2']
通过连接到字符串将 ASCII 字符更改为文本。
paths = ''.join([chr(i) for i in input if chr(i) not in '<,'])
现在路径字符串如下所示:
paths=/notpath/exclude>/ev/path1>/ev/path2>
现在我想排除掉不需要的初始路径,保存剩下的路径
start = len(paths[0:paths.find(">")]) + 1
f_paths = []
g=''
for x in paths[start:]:
if x != '>':
g = g + x
else:
f_paths.append(g)
g = ''
输出是预期的,但必须有一种更优化的方法来执行 for 循环,问题是我不知道如何。
你可以这样做:
paths='/notpath/exclude>/ev/path1>/ev/path2>'
f_paths = paths.split('>')[1:-1]
输出:
['/ev/path1', '/ev/path2']
您可以使用 regex
:
import re
paths = '/notpath/exclude>/ev/path1>/ev/path2>'
print(re.findall(r'(?<=>).*?(?=>)', paths))
# ['/ev/path1', '/ev/path2']