Python: How to join string elements in list with complex notation
Question:
Edited to show more accurate strings
I have a long list with the following grouping structure. I am showing just two groups, but in reality there are 300. The strings can be any random collection of numbers and letters:
["['P431', 'N7260', 'K492'], ['R109', 'T075X9A'], ['U8154', 'C861']",
"['R878X8', 'T61'], ['F6332', 'Q7979'], ['L520'], ['B7939', 'K5132']"]
For each group in the list, I want the final output to look like the following:
[['P431 N7260 K492 R109 T075X9A', 'U8154 C861'],
['R878X8 T61 F6332 Q7979 L520', 'B7939 K5132']]
So for each group in the list, I want to join all of the elements in every sub-list except the last sub-list, then separately join all of the elements in the last sub-list, and finally add these both together into a single sub-list, each of which will now have just two elements. Thank you in advance as I am a beginner.
Answers:
I’m assuming you’have some typos in your string (missing '
). You can try:
from ast import literal_eval
l = [
"['cat', 'dog', 'mouse'], ['squirrel', 'lion'], ['fox', 'rabbit']",
"['chipmunk', 'lion'], ['hyena', 'wolf'], ['owl', 'sloth']",
]
l = [
[" ".join(w for ll in subl for w in ll), " ".join(last)]
for s in l
for *subl, last in [literal_eval(s)]
]
print(l)
Prints:
[
['cat dog mouse squirrel lion', 'fox rabbit'],
['chipmunk lion hyena wolf', 'owl sloth']
]
use ast.literal_eval for construct list from string, then join with space all except the last and then union all together
import ast
result = []
for group in data:
subgroups = ast.literal_eval(group)
# join all except last
combined = [' '.join(subgroup) for subgroup in subgroups[:-1]]
# last append
last_subgroup = ' '.join(subgroups[-1])
result.append([' '.join(combined), last_subgroup])
print(result)
You can try this:
from ast import literal_eval
example=[
"['P431', 'N7260', 'K492'], ['R109', 'T075X9A'], ['U8154', 'C861']",
"['R878X8', 'T61'], ['F6332', 'Q7979'], ['L520'], ['B7939', 'K5132']",
"['a','b','c'], 'not a list'"
]
for st in example:
*first_grp,last=literal_eval(st)
if any(not isinstance(s, list) for s in (first_grp, last)):
continue
print([" ".join([e for r in first_grp for e in r]), " ".join(last)])
Prints:
['P431 N7260 K492 R109 T075X9A', 'U8154 C861']
['R878X8 T61 F6332 Q7979 L520', 'B7939 K5132']
This is similar to Andrej Kesely solution but may overcome the ‘random strings’ you describe with a type check.
Edited to show more accurate strings
I have a long list with the following grouping structure. I am showing just two groups, but in reality there are 300. The strings can be any random collection of numbers and letters:
["['P431', 'N7260', 'K492'], ['R109', 'T075X9A'], ['U8154', 'C861']",
"['R878X8', 'T61'], ['F6332', 'Q7979'], ['L520'], ['B7939', 'K5132']"]
For each group in the list, I want the final output to look like the following:
[['P431 N7260 K492 R109 T075X9A', 'U8154 C861'],
['R878X8 T61 F6332 Q7979 L520', 'B7939 K5132']]
So for each group in the list, I want to join all of the elements in every sub-list except the last sub-list, then separately join all of the elements in the last sub-list, and finally add these both together into a single sub-list, each of which will now have just two elements. Thank you in advance as I am a beginner.
I’m assuming you’have some typos in your string (missing '
). You can try:
from ast import literal_eval
l = [
"['cat', 'dog', 'mouse'], ['squirrel', 'lion'], ['fox', 'rabbit']",
"['chipmunk', 'lion'], ['hyena', 'wolf'], ['owl', 'sloth']",
]
l = [
[" ".join(w for ll in subl for w in ll), " ".join(last)]
for s in l
for *subl, last in [literal_eval(s)]
]
print(l)
Prints:
[
['cat dog mouse squirrel lion', 'fox rabbit'],
['chipmunk lion hyena wolf', 'owl sloth']
]
use ast.literal_eval for construct list from string, then join with space all except the last and then union all together
import ast
result = []
for group in data:
subgroups = ast.literal_eval(group)
# join all except last
combined = [' '.join(subgroup) for subgroup in subgroups[:-1]]
# last append
last_subgroup = ' '.join(subgroups[-1])
result.append([' '.join(combined), last_subgroup])
print(result)
You can try this:
from ast import literal_eval
example=[
"['P431', 'N7260', 'K492'], ['R109', 'T075X9A'], ['U8154', 'C861']",
"['R878X8', 'T61'], ['F6332', 'Q7979'], ['L520'], ['B7939', 'K5132']",
"['a','b','c'], 'not a list'"
]
for st in example:
*first_grp,last=literal_eval(st)
if any(not isinstance(s, list) for s in (first_grp, last)):
continue
print([" ".join([e for r in first_grp for e in r]), " ".join(last)])
Prints:
['P431 N7260 K492 R109 T075X9A', 'U8154 C861']
['R878X8 T61 F6332 Q7979 L520', 'B7939 K5132']
This is similar to Andrej Kesely solution but may overcome the ‘random strings’ you describe with a type check.