Home > Software design >  Delimiter List of list
Delimiter List of list

Time:10-24

I have a list of lists and need to change the delimiter to '|':

my_list_of_lists = [['1001100', 'xxx', '', '1100', 'PAR_200', '21651947694', '0', '0', '20210301', '20210331', '1630,51', '1', ''],
['1001110', 'sss', '', '1110', 'C-200', '20210307', '1630,51', '1', '7252048000136', ''],
['1001115', 'ddd', '', '1115', '21', '', 'NO6520058050362', '0', '08', '110433', '1630,51', '1', '', ''],
['1001120', 'fff', '', '1120', 'MG', '', '', '73895137480', ''],
['101100', 'ees', '', '1100', 'PAR_200', '21653346587', '0', '0', '20210301', '20210331', '1013,84', '1', ''],
['101110', 'fsc', '', '1110', 'C-200', '20210307', '1013,84', '1', '7252048000136', ''],
['101115', 'fer', '', '1115', '1', '', 'LI1600081181694N2K346', '0', '06', '110433', '1013,84', '1', '', ''],
['101120', 'ter', '', '1120', 'MG', '', '', '17651232321', '']]

When I try this with a single list, works:

my_list = ['1001100', 'xxx', '', '1100', 'PAR_200', '21651947694', '0', '0', '20210301', '20210331', '1630,51', '1', '']

delim = "|"

res = ''

for lists in my_list:
    res = res   str(lists)   delim

print(res)

1001100|xxx||1100|PAR_200|21651947694|0|0|20210301|20210331|1630,51|1||

But I think I'm missing something on the list comprehension to do this in a list of lists:

delim = "|"

res = ''

for sub_lst in my_list_of_lists:
    for lists in sub_lst:
        res = res   str(lists)   delim

Desired output:

1001100|xxx||1100|PAR_200|21651947694|0|0|20210301|20210331|1630,51|1||
1001110|sss||1110|C-200|20210307|1630,51|1|7252048000136||
1001115|ddd||1115|21||NO6520058050362|0|08|110433|1630,51|1|||
....

What am I doing wrong?

CodePudding user response:

use join

lst = [['1001100', 'xxx', '', '1100', 'PAR_200', '21651947694', '0', '0', '20210301', '20210331', '1630,51', '1', ''],
['1001110', 'sss', '', '1110', 'C-200', '20210307', '1630,51', '1', '7252048000136', ''],
['1001115', 'ddd', '', '1115', '21', '', 'NO6520058050362', '0', '08', '110433', '1630,51', '1', '', ''],
['1001120', 'fff', '', '1120', 'MG', '', '', '73895137480', ''],
['101100', 'ees', '', '1100', 'PAR_200', '21653346587', '0', '0', '20210301', '20210331', '1013,84', '1', ''],
['101110', 'fsc', '', '1110', 'C-200', '20210307', '1013,84', '1', '7252048000136', ''],
['101115', 'fer', '', '1115', '1', '', 'LI1600081181694N2K346', '0', '06', '110433', '1013,84', '1', '', ''],
['101120', 'ter', '', '1120', 'MG', '', '', '17651232321', '']]

out = ['|'.join(l) for l in lst]
print(out)

output

['1001100|xxx||1100|PAR_200|21651947694|0|0|20210301|20210331|1630,51|1|', '1001110|sss||1110|C-200|20210307|1630,51|1|7252048000136|', '1001115|ddd||1115|21||NO6520058050362|0|08|110433|1630,51|1||', '1001120|fff||1120|MG|||73895137480|', '101100|ees||1100|PAR_200|21653346587|0|0|20210301|20210331|1013,84|1|', '101110|fsc||1110|C-200|20210307|1013,84|1|7252048000136|', '101115|fer||1115|1||LI1600081181694N2K346|0|06|110433|1013,84|1||', '101120|ter||1120|MG|||17651232321|']
  • Related