1

I have list containing some paths:

['folder1/folder2/Module1', 'folder4/folder5/Module2', 'folder7/folder8/Module3', 'folder12/folder13/Module4', 'folder17/folder20/folder50/Module5' .. etc]

What would be the best way to extract each element of that list and create new list or some other place to store that path with it's specific name?

Mu current code for going through each element of the list and storing it one by one, but I can't generate new list for each element, not sure if that is even possible:

for j in range(len(listOfPaths)):
  del pathList[:]
  path = listOfPaths[j]
  pathList.append(path)

So to clarify, at the end what I need is to get one list list[Module1] that contains only 'folder1/folder2/Module1', and second one list[Module2] with only path to Module2, etc...

John
  • 230
  • 2
  • 12
  • It might be helpful to extend your answer a bit, with a better example of what you want your output to look like. Usually you access lists by index (number), so something like `list[Module2]` would be `list[1]`. If you want something more specific, like `list[Module2]`, you probably need a dict. – tobii May 28 '21 at 12:09

3 Answers3

1

Check this?

temp=['folder1/folder2/Module1', 'folder4/folder5/Module2', 'folder7/folder8/Module3', 'folder12/folder13/Module4', 'folder17/folder20/folder50/Module5']
# List initialization
Output = [] 
  
# Using Iteration to convert 
# element into list of list
for elem in temp:
    temp3=[]
    temp3.append(elem)
    Output.append(temp3)
  
# printing
print(Output)

Output:

[['folder1/folder2/Module1'], ['folder4/folder5/Module2'], ['folder7/folder8/Module3'], ['folder12/folder13/Module4'], ['folder17/folder20/folder50/Module5']]
  • yeah but this means you will get new list with all elements that temp contains so basically you will get temp again :) – John May 28 '21 at 11:41
  • What I need is to get one list list[Module1] that contains only 'folder1/folder2/Module1', and second one list[Module2] with only path to Module2, etc... – John May 28 '21 at 11:42
  • So you need to split one list into several lists –  May 28 '21 at 12:23
  • Yes, something like that – John May 28 '21 at 13:51
1

It might be better to use a Dictionary here, instead of a list.

#!/usr/bin/env python3
import os

paths = []

paths.append("folder1/subfolderA/Module1")
paths.append("folder2/subfolderB/Module1")
paths.append("folder3/subfolderC/Module1")

paths.append("folder4/subfolderD/Module2")
paths.append("folder5/subfolderE/Module2")

paths.append("folder6/subfolderF/Module50")



# create an empty dictionary
modulesDict = {}
# it will look like this:
#  "ModuleX" -> ["path1/to/ModuleX", "path2/to/ModuleX", ...]
#  "ModuleY" -> ["path3/to/ModuleY", "path4/to/ModuleY", ...]

for path in paths: # loop over original list of paths
    # take only the "ModuleX" part
    moduleName = os.path.basename(os.path.normpath(path))
    # check if its already in our dict or not
    if moduleName in modulesDict:
        # add the path to the list of paths for that module
        modulesDict.get(moduleName).append(path)
    else:
        # create an new list, with only one element (only the path)
        modulesDict[moduleName] = [path]


print(modulesDict)


OUTPUT: (formatted a bit)

{
'Module1':
    ['folder1/subfolderA/Module1', 'folder2/subfolderB/Module1', 'folder3/subfolderC/Module1'],
'Module2':
    ['folder4/subfolderD/Module2', 'folder5/subfolderE/Module2'],
'Module50':
    ['folder6/subfolderF/Module50']
}


tobii
  • 71
  • 3
0

I think you can try this:

input=["stra", "strb"]

output =list(map(lambda x: [x], input))