dani
dani

Reputation: 23

Python concatenate char to list

The initial input is:

input = [60, 20, 50, 70, …, 90]  # ASCII characters

I want an output like this:

f_paths = ['/ev/path1', '/ev/path2']

The ASCII characters are changed to text by concatenating to a string.

paths = ''.join([chr(i) for i in input if chr(i) not in '<,'])

Now paths string looks like this:

paths=/notpath/exclude>/ev/path1>/ev/path2>

Now I want to exclude the initial path that is not needed and save the remaining paths

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 = ''

Output is the expected one but there has to be a more optimal way to do the for loop, the problem is I don't know how.

Upvotes: 1

Views: 115

Answers (2)

Austin
Austin

Reputation: 26039

You could use a regex:

import re

paths = '/notpath/exclude>/ev/path1>/ev/path2>'
print(re.findall(r'(?<=>).*?(?=>)', paths))

# ['/ev/path1', '/ev/path2']

Upvotes: 0

Joe
Joe

Reputation: 12417

You could do so:

paths='/notpath/exclude>/ev/path1>/ev/path2>'
f_paths = paths.split('>')[1:-1]

Output:

['/ev/path1', '/ev/path2']

Upvotes: 1

Related Questions