I have a below list.
['A_1', 'A_2', 'A_3', 'A_4', 'B_1','B_2', 'C_1', 'C_2', 'C_3']
I need the output in below format.
[['A_1', 'A_2', 'A_3', 'A_4'], ['B_1', 'B_2'], ['C_1', 'C_2', 'C_3']]
I am very new to Python, is there any way to achieve the above output that it would be really helpful.
CodePudding user response:
This can be achieved directly with a dictionary combined with a loop:
- store each type of data in the form of "letter_", e.g. {"A_": ["A_1", "A_2"]}
- Loop through each data in the list, if the type is not in the dictionary, create a list with the prefix type of this data, otherwise add it directly to the list of this type
- Finally, directly convert the values of the dictionary into a list to achieve the requirement
category = {}
lst = ['A_1', 'A_2', 'A_3', 'A_4', 'B_1','B_2', 'C_1', 'C_2', 'C_3']
for item in lst:
prefix = item.split("_")[0] "_" # Example of prefix values: A_, B_,...
if prefix not in category:
category[prefix] = [item]
else:
category[prefix].append(item)
print(list(category.values()))
# [['A_1', 'A_2', 'A_3', 'A_4'], ['B_1', 'B_2'], ['C_1', 'C_2', 'C_3']]
CodePudding user response:
I provided a easily understandable way (but requires more lines than other advanced thecniques)
You can use dictionary
, in particular, I used defaultdict
here to set default value as a list ([]).
I grouped the list by first letter in a for loop
, as follows:
from collections import defaultdict
l = ['A_1', 'A_2', 'A_3', 'A_4', 'B_1', 'B_2', 'C_1', 'C_2', 'C_3']
group = defaultdict(list)
for item in l:
first_letter = item[0] # saving items by first letter
group[first_letter].append(item)
result = list(group.values())
print(result)
# [['A_1', 'A_2', 'A_3', 'A_4'], ['B_1', 'B_2'], ['C_1', 'C_2', 'C_3']]
CodePudding user response:
You can use itertools.groupby
for that. This assumes that the input is sorted, as in your example.
from itertools import groupby
lst = ['A_1', 'A_2', 'A_3', 'A_4', 'B_1', 'B_2', 'C_1', 'C_2', 'C_3']
output = [list(g) for _, g in groupby(lst, key=lambda x: x[0])]
print(output) # [['A_1', 'A_2', 'A_3', 'A_4'], ['B_1', 'B_2'], ['C_1', 'C_2', 'C_3']]
If for some reason you don't want to use import
but only use built-ins,
lst = ['A_1', 'A_2', 'A_3', 'A_4', 'B_1', 'B_2', 'C_1', 'C_2', 'C_3']
output = {}
for x in lst:
if x[0] in output:
output[x[0]].append(x)
else:
output[x[0]] = [x]
print(list(output.values()))
Note that with an input list of ['A_1', 'B_1', 'A_2']
the two approaches will result in different outputs.