I have a nested list of strings:
d = [['one\\alpha','two\\beta','three\\gamma'],['foo\\data','bar\\params']]
What I want to do is given the list d return a list:
data = [['alpha','beta','gamma'],['data','params']]
That is iterate through each element of the each inner list in d and return the substring after the \\.
My attempt at a solution is:
data = []
for n in range(len(d)):
for m in range(len(d[n])):
a = str(d[n][m])
data.append(a.split("\\")[1])
Which produces: data = ['alpha', 'beta', 'gamma', 'data', 'params']
Which produces the correct strings in the correct sequence, but i lose the nature of how the list d was nested. Is there anyway to produce the list data given d that keeps the nested structure?
Edit:
I've actually just managed to solve this using:
[[d[n][m].split("\\")[1] for m in range(len(d[n]))] for n in range(len(d))]
rangeover an objects length to loop over it