Home > front end >  Python - Join list of lists with list of strings
Python - Join list of lists with list of strings

Time:12-17

I have python script that reads a .txt. file and appends items to a two different lists:

list_data_type = [
  ['void'], ['uint8'], 
  [['uint8'], ['uint32']], ['void'], 
  ['void'], ['void'], ['void']
]

list_arg_parameter = [
  [None], ['f_MbistTestType_u8'], 
  ['f_MbistTestType_u8', 'f_chip_id_u32'], [None], 
  [None], [None], [None]
]

I want to join corresponding items of both list, which I know can be done with zip function, but I want to print the result of both lists in a .csv file, and I want that those two list with 2 elements would be written into same slot and it would look like this:

file_name_1;function_name_1; void (None)
file_name_1;function_name_2; uint8 f_MbistTestType_u8
file_name_1;function_name_3; uint8 f_MbistTestType_u8, uint32 f_chip_id_u32

Also I don't want any "[]" or "''" signs. Right now I tried to convert both lists to a string, strip all unnecessary brackets, convert those strings back to list via this function:

def Convert(string):
li = list(string.split(" "))
return li

And then use simple for loop to make pair of elements:

for elements in zip(list_data_type_strpd, list_arg_parameter_strpd): 
    list_func_arg_combined.append(' '.join(elements))

But the result looks like this:

'void '
'uint8 f_MbistTestType_u8'
'uint8 f_MbistTestType_u8'
'uint32 f_chip_id_32'
'void '
'void '
'void '
'void '

And I would like it, like this:

'void '
'uint8 f_MbistTestType_u8'
'uint8 f_MbistTestType_u8, uint32 f_chip_id_32'
'void '
'void '
'void '
'void '

Maybe I turn those list to string unnecessarily? Maybe there is a easier way to accomplish my goal? Any help, critique or different solutions are welcome.

CodePudding user response:

Here's my solution to print the lines according to how you want them:

for data_types, arg_parameters in zip(list_data_type, list_arg_parameter):
    print_line = ""
    for data_type, arg_parameter in zip(data_types, arg_parameters):
        if print_line:
            print_line  = ", "
        # You could extract the next line to the `convert` function if you wish to do so
        print_data_type = str(data_type).lstrip("['").rstrip("]'")
        print_line  = f"{print_data_type} {arg_parameter}"
    print(print_line)

However, I think there's an error in how list_data_type is initialised. Specifically, the 3rd element in list_data_type should be a list of strings, not a list of lists. Specifically,

# This requires us to have a `hacky` convert function
list_data_type = [
  ['void'], ['uint8'], 
  [['uint8'], ['uint32']], ['void'], 
  ['void'], ['void'], ['void']
]
# The proper one would be a list of strings instead
list_data_type = [
  ['void'], ['uint8'], 
  ['uint8', 'uint32'], ['void'],  # Note the difference in this line
  ['void'], ['void'], ['void']
]

Since you mentioned that you have a python script that reads a txt file and initialises list_data_type, you might want to fix that. Once that is fixed, you can remove the convert function and it will print your data in your output nicely.

CodePudding user response:

Using a nested list comprehension and zip (one level to merge the two lists, one level to zip the sublists, one level to extract the strings from the lowest level of list):

out = [', '.join([' '.join([z[0] if isinstance(z, list) else z for z in y])
                  if y[1] else y[0]
                  for y in (zip(*x))])
       for x in zip(list_data_type, list_arg_parameter)]

Output:

['void',
 'uint8 f_MbistTestType_u8',
 'uint8 f_MbistTestType_u8, uint32 f_chip_id_u32',
 'void',
 'void',
 'void',
 'void']
  • Related