Stack Overflow Asked on January 10, 2021
I have a list like this:
['AA/usermanagedmanualreport',
'AccountingDocuments/BPC/COPA/NGAP/Tableau',
'AccountingDocuments/BW',
'AccountingDocuments/DOMS/POS/RFS/SAPBPC/COPA/NGAP/Tableau',
'AccountsPayable/Akritiv']
I’m trying to get it like this:
['AA/usermanagedmanualreport',
'AccountingDocuments/BPC',
'AccountingDocuments/COPA',
'AccountingDocuments/NGAP',
'AccountingDocuments/Tableau',
'AccountingDocuments/BW',
'AccountingDocuments/DOMS',
'AccountingDocuments/POS',
'AccountingDocuments/RFS',
'AccountingDocuments/SAPBPC',
'AccountingDocuments/COPA',
'AccountingDocuments/NGAP',
'AccountingDocuments/Tableau',
'AccountsPayable/Akritiv']
I basically just need to split each list element in to separate elements based on the first value in the list and the "/" separator.
Tried something like this but it doesn’t do quite what I want:
[l.split('/') for l in ','.join(myList).split('|')]
Any ideas?
The easiest way is probably to use a loop:
parts = []
for x in myList:
first, *rest = x.split("/")
parts += [first + "/" + part for part in rest]
print(parts)
Correct answer by Aplet123 on January 10, 2021
One-liner for fun:
>>> l = ['AA/usermanagedmanualreport',
... 'AccountingDocuments/BPC/COPA/NGAP/Tableau',
... 'AccountingDocuments/BW',
... 'AccountingDocuments/DOMS/POS/RFS/SAPBPC/COPA/NGAP/Tableau',
... 'AccountsPayable/Akritiv']
>>> result = [f'{a}/{x}' for a, *b in (item.split('/') for item in l) for x in b]
>>> print(*result, sep='n')
AA/usermanagedmanualreport
AccountingDocuments/BPC
AccountingDocuments/COPA
AccountingDocuments/NGAP
AccountingDocuments/Tableau
AccountingDocuments/BW
AccountingDocuments/DOMS
AccountingDocuments/POS
AccountingDocuments/RFS
AccountingDocuments/SAPBPC
AccountingDocuments/COPA
AccountingDocuments/NGAP
AccountingDocuments/Tableau
AccountsPayable/Akritiv
Answered by TigerhawkT3 on January 10, 2021
You can do something like this:
l=['AA/usermanagedmanualreport',
'AccountingDocuments/BPC/COPA/NGAP/Tableau',
'AccountingDocuments/BW',
'AccountingDocuments/DOMS/POS/RFS/SAPBPC/COPA/NGAP/Tableau',
'AccountsPayable/Akritiv']
def div(x):
s=x.split('/')
first=s[0]
rest=s[1:]
return [first+'/'+i for i in rest]
res=[]
for i in l:
if i.count('/')<2:
res.append(i)
else:
res.extend(div(i))
>>>print(res)
['AA/usermanagedmanualreport', 'AccountingDocuments/BPC', 'AccountingDocuments/COPA', 'AccountingDocuments/NGAP', 'AccountingDocuments/Tableau', 'AccountingDocuments/BW', 'AccountingDocuments/DOMS', 'AccountingDocuments/POS', 'AccountingDocuments/RFS', 'AccountingDocuments/SAPBPC', 'AccountingDocuments/COPA', 'AccountingDocuments/NGAP', 'AccountingDocuments/Tableau', 'AccountsPayable/Akritiv']
Answered by IoaTzimas on January 10, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP