简体   繁体   English

删除嵌套列表中某些字符之前的所有内容(Python)

[英]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"]] [ “BLAHBLAH \\桌面”, “BLAHBLAH \\文档”, “BLAHBLAH \\西元”],[ “BLAHBLAH \\照片管理”, “BLAHBLAH \\文件夹”, “BLAHBLAH \\音乐”]

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. 这是在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 我知道你必须使用带有反斜杠的rfind,但是我无法遍历嵌套列表以维护嵌套列表结构

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). 如果您的文件名在myList ,那么应该这样做,并且也可以独立于平台(不同的操作系统使用不同的文件夹分隔符,但os.path模块会为您处理)。

import os

[[os.path.basename(x) for x in sublist] for sublist in myList]
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']]

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]

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)

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 荣誉(和+1)来

  • @Junuxx who was first with the filename solution, @Junuxx是第一个使用文件名解决方案的人,
  • to @Ashwini Chaudary who got a more general solution if these are not filenames, and 到@Ashwini Chaudary,如果这些不是文件名,那么他们会获得更通用的解决方案
  • to @mfusennegger who, I think, is making a joke. @mfusennegger,我认为,他正在开个玩笑。

I dont have access to a computer with python atm, but the following should work: 我没有访问python atm的计算机,但以下应该工作:

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("/"):])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM