user2315
user2315

Reputation: 831

Remove everything before certain character in nested lists (Python)

Lets suppose I have a list such as:

[["BLAHBLAH\Desktop","BLAHBLAH\Documents","BLAHBLAH\Vids"],["BLAHBLAH\Pics","BLAHBLAH\Folder","BLAHBLAH\Music"]]

And I wanted an output that would look like

[["Desktop","Documents","Vids"],["Pics","Folder","Music"]]

How would I go about doing so? This is in Python. I know you would have to use rfind with the backslashes but I'm having trouble iterating through the nested lists to maintain that nested list structure

Upvotes: 2

Views: 359

Answers (6)

Junuxx
Junuxx

Reputation: 14261

If your filenames are in myList, this should do it, and platform-independently too (different OSes use different folder separators, but the os.path module takes care of that for you).

import os

[[os.path.basename(x) for x in sublist] for sublist in myList]

Upvotes: 6

daedalus
daedalus

Reputation: 10923

The number of answers is just great. They all work in different contexts. I am just adding this to the list:

outer = [["BLAHBLAH\Desktop","BLAHBLAH\Documents","BLAHBLAH\Vids"],
         ["BLAHBLAH\Pics","BLAHBLAH\Folder","BLAHBLAH\Music"]]

purged = [ [ item[ item.find("\\")+1: ]
             for item in inner ]
           for inner in outer ]

Kudos (and +1) to

  • @Junuxx who was first with the filename solution,
  • to @Ashwini Chaudary who got a more general solution if these are not filenames, and
  • to @mfusennegger who, I think, is making a joke.

Upvotes: 1

mfussenegger
mfussenegger

Reputation: 3971

Something like this?

from unittest import TestCase
import re


def foo(l):
    result = []
    for i in l:
        if isinstance(i, list):
            result.append(foo(i))
        else:
            result.append(re.sub('.*\\\\', '', i))
    return result


class FooTest(TestCase):
    def test_foo(self):
        arg = ['DOC\\Desktop', 'BLAH\\FOO', ['BLAH\\MUSIC', 'BLABLA\\TEST']]
        expected = ['Desktop', 'FOO', ['MUSIC', 'TEST']]
        actual = foo(arg)
        self.assertEqual(expected, actual)

Upvotes: 1

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250971

lis=[["BLAHBLAH\Desktop","BLAHBLAH\Documents","BLAHBLAH\Vids"],["BLAHBLAH\Pics","BLAHBLAH\Folder","BLAHBLAH\Music"]]

def stripp(x):
    return x.strip('BLAHBLAH\\')

lis=[list(map(stripp,x)) for x in lis]
print(lis)                   

output:

[['Desktop', 'Documents', 'Vids'], ['Pics', 'Folder', 'Music']]

Upvotes: 3

IT Ninja
IT Ninja

Reputation: 6430

I dont have access to a computer with python atm, but the following should work:

List=[["BLAHBLAH\Desktop","BLAHBLAH\Documents","BLAHBLAH\Vids"],["BLAHBLAH\Pics","BLAHBLAH\Folder","BLAHBLAH\Music"]]
final=[]
for varv in List:
    x=varv
    for sub_val in x:
        final.append(sub_val[sub_val.find("/"):])

Upvotes: 0

gypaetus
gypaetus

Reputation: 7349

You should use list comprehensions:

NestedList = [["BLAHBLAH\Desktop","BLAHBLAH\Documents","BLAHBLAH\Vids"],["BLAHBLAH\Pics","BLAHBLAH\Folder","BLAHBLAH\Music"]]
output = [[os.path.basename(path) for path in li] for li in NestedList]

Upvotes: 2

Related Questions