How do I print a list of lists where the inner lists contain both strings and lists of strings?
- Parm
- 2018-07-13 00:37
- 4
For example,
list1 = [ ['str1', 'str2', 'str3'], [['str4', 'str5'], 'str6', 'str7', 'str8'], ['str9', 'str10']]
I would like list1 to output as follows:
str1 str2 str3 str4 str5 str6 str7 str8 str9 str10
This way each of the inner lists belonging to the first layer of inner lists are on their own lines.
In the above example I would call the section depicted below an inner list of the first layer:
['str1', 'str2', 'str3']
However in the following section I would say str4 and str5 are in an inner list belonging to the second layer. In other words they belong to an inner list of an inner list.
[['str4', 'str5'], 'str6', 'str7', 'str8']
4 Answers
Use a recursive function to deal with any depth of sub-lists, but make the printing of newline characters after each sub-list optional and only enable it at the first call.
list1 = [ ['str1', 'str2', 'str3'], [['str4', 'str5'], 'str6', 'str7', 'str8'], ['str9', 'str10']] def p(l, newline=True): output = '' for i in l: if isinstance(i, str): output += i + ' ' else: p(i, False) if newline: print() print(output, end='') p(list1)
This outputs:
str1 str2 str3 str4 str5 str6 str7 str8 str9 str10
nested list python, Nested List Comprehensions in Python filter_none edit close play_arrow link brightness_4 code. But in our case, the expression itself is a list comprehension. edit close. play_arrow. link brightness_4 code. But in our case, the expression itself is a list comprehension. Hence expression = [0, Nested List Comprehensions in Python filter_none edit close play_arrow link brightness_4 code. But in our case, the expression itself is a list comprehension. edit close. play_arrow. link brightness_4 code. But in our case, the expression itself is a list comprehension. Hence expression = [0,
blhsing
2018-07-13 01:02
Firstly, you need to loop through the first inner layer then flatten the elements inside it.
Then print it in order with join function. Detail below.
list1 = [ ['str1', 'str2', 'str3'], [['str4', 'str5'], 'str6', 'str7', 'str8'], ['str9', 'str10']] def flatten(lst): if lst==[]: return lst else: if isinstance(list[0], list): return flatten(lst[0])+flatten(lst[1:]) else: return [lst[0],]+flatten(lst[1:]) def print_it(lst_rino): for lst in lst_rino: new_lst=flatten(lst) print(" ".join(new_lst)) print_it(list1) The Output: str1 str2 str3 str4 str5 str6 str7 str8 str9 str10
python list of lists, It is quite easy to create list of lists in Python. You just need to use list’s append method to create list of lists. Here is simple example to create list of lists in Python. list1= [1,2,3,4] list2= [5,6,7,8] listoflists= [] listoflists.append (list1) listoflists.append (list2) print ("List of Lists:",listoflists) script.py. It is quite easy to create list of lists in Python. You just need to use list’s append method to create list of lists. Here is simple example to create list of lists in Python. list1= [1,2,3,4] list2= [5,6,7,8] listoflists= [] listoflists.append (list1) listoflists.append (list2) print ("List of Lists:",listoflists) script.py.
Marcus.Aurelianus
2018-07-13 03:03
Still new to python but cant this be solved with simple string concatenation and list elements?
something along the lines of
list1 = [['str1', 'str2', 'str3'],[['str4', 'str5'], 'str6', 'str7', 'str8'],['str9', 'str10']] print(str(list1[0]) + '\n' + str(list1[1]) + '\n' + str(list1[2]))
output is exactly what op asked for?
['str1', 'str2', 'str3'] [['str4', 'str5'], 'str6', 'str7', 'str8'] ['str9', 'str10']
create list of lists python, That will create a list (a type of mutable array in python) called my_list with the output of the np.getfromtext () method in the first 2 indexes. Just came across the same issue today In order to create a list of lists you will have firstly to store your data, array, or other type of variable into a list. That will create a list (a type of mutable array in python) called my_list with the output of the np.getfromtext () method in the first 2 indexes. Just came across the same issue today In order to create a list of lists you will have firstly to store your data, array, or other type of variable into a list.
Eric Digiorgio
2018-07-13 01:04
You can use
itertools.chain
for this:11. Lists, You can use itertools.chain for this: list1 = [['str1', 'str2', 'str3'], [['str4', 'str5'], 'str6', 'str7', 'str8'], ['str9', 'str10']] from itertools import chain def Lists and strings — and other collections that maintain the order of their items Each time through the loop, the variable i is used as an index into the list, printing the i'th Although a list can contain another list, the nested list still counts as a single Since variables refer to objects, if we assign one variable to another, both
jpp
2018-07-13 00:49