Flatten all inner lists that do not have sublists
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
As the title says, I would like to flatten all inner lists, excluded the ones that do not have sublists. For example, from
lis = a, b, d, f, c, k, h, l, e, a;
I would like to have
myFlatten[lis]
a, b, d, f, c, k, h, l, e, a
I have a solution, but probably is inefficient and not elegant.
The steps are the following:
- change the
Head
of allList
subparts that do not have members of typeList
, to something else, saymyList
Flatten
the resulting expression- change back
myList
toList
this is my implementation:
notListQ[z_] := Head[z] =!= List;
myFlatten[list_List] := Module[myList, tempList,
tempList = list /. List[y__?notListQ] -> myList[y];
tempList = Flatten[tempList];
tempList /. myList -> List
]
This obviously can be shortened to
myFlatten[list_List] := Module[myList,
Flatten[list /. List[y__?notListQ] -> myList[y]] /. myList -> List
]
Any errors in my implementation?
Better solutions?
Thank you
list-manipulation
add a comment |Â
up vote
3
down vote
favorite
As the title says, I would like to flatten all inner lists, excluded the ones that do not have sublists. For example, from
lis = a, b, d, f, c, k, h, l, e, a;
I would like to have
myFlatten[lis]
a, b, d, f, c, k, h, l, e, a
I have a solution, but probably is inefficient and not elegant.
The steps are the following:
- change the
Head
of allList
subparts that do not have members of typeList
, to something else, saymyList
Flatten
the resulting expression- change back
myList
toList
this is my implementation:
notListQ[z_] := Head[z] =!= List;
myFlatten[list_List] := Module[myList, tempList,
tempList = list /. List[y__?notListQ] -> myList[y];
tempList = Flatten[tempList];
tempList /. myList -> List
]
This obviously can be shortened to
myFlatten[list_List] := Module[myList,
Flatten[list /. List[y__?notListQ] -> myList[y]] /. myList -> List
]
Any errors in my implementation?
Better solutions?
Thank you
list-manipulation
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
As the title says, I would like to flatten all inner lists, excluded the ones that do not have sublists. For example, from
lis = a, b, d, f, c, k, h, l, e, a;
I would like to have
myFlatten[lis]
a, b, d, f, c, k, h, l, e, a
I have a solution, but probably is inefficient and not elegant.
The steps are the following:
- change the
Head
of allList
subparts that do not have members of typeList
, to something else, saymyList
Flatten
the resulting expression- change back
myList
toList
this is my implementation:
notListQ[z_] := Head[z] =!= List;
myFlatten[list_List] := Module[myList, tempList,
tempList = list /. List[y__?notListQ] -> myList[y];
tempList = Flatten[tempList];
tempList /. myList -> List
]
This obviously can be shortened to
myFlatten[list_List] := Module[myList,
Flatten[list /. List[y__?notListQ] -> myList[y]] /. myList -> List
]
Any errors in my implementation?
Better solutions?
Thank you
list-manipulation
As the title says, I would like to flatten all inner lists, excluded the ones that do not have sublists. For example, from
lis = a, b, d, f, c, k, h, l, e, a;
I would like to have
myFlatten[lis]
a, b, d, f, c, k, h, l, e, a
I have a solution, but probably is inefficient and not elegant.
The steps are the following:
- change the
Head
of allList
subparts that do not have members of typeList
, to something else, saymyList
Flatten
the resulting expression- change back
myList
toList
this is my implementation:
notListQ[z_] := Head[z] =!= List;
myFlatten[list_List] := Module[myList, tempList,
tempList = list /. List[y__?notListQ] -> myList[y];
tempList = Flatten[tempList];
tempList /. myList -> List
]
This obviously can be shortened to
myFlatten[list_List] := Module[myList,
Flatten[list /. List[y__?notListQ] -> myList[y]] /. myList -> List
]
Any errors in my implementation?
Better solutions?
Thank you
list-manipulation
list-manipulation
asked 38 mins ago
enzotib
477415
477415
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
ReleaseHold[Flatten[MapAt[Hold, lis, Position[lis, _List?VectorQ]]]]
a, b, d, f, c, k, h, l, e, a
1
Another variant isReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
â Carl Woll
3 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
ReleaseHold[Flatten[MapAt[Hold, lis, Position[lis, _List?VectorQ]]]]
a, b, d, f, c, k, h, l, e, a
1
Another variant isReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
â Carl Woll
3 mins ago
add a comment |Â
up vote
4
down vote
ReleaseHold[Flatten[MapAt[Hold, lis, Position[lis, _List?VectorQ]]]]
a, b, d, f, c, k, h, l, e, a
1
Another variant isReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
â Carl Woll
3 mins ago
add a comment |Â
up vote
4
down vote
up vote
4
down vote
ReleaseHold[Flatten[MapAt[Hold, lis, Position[lis, _List?VectorQ]]]]
a, b, d, f, c, k, h, l, e, a
ReleaseHold[Flatten[MapAt[Hold, lis, Position[lis, _List?VectorQ]]]]
a, b, d, f, c, k, h, l, e, a
answered 31 mins ago
Coolwater
13.5k32150
13.5k32150
1
Another variant isReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
â Carl Woll
3 mins ago
add a comment |Â
1
Another variant isReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
â Carl Woll
3 mins ago
1
1
Another variant is
ReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
â Carl Woll
3 mins ago
Another variant is
ReleaseHold @ Flatten @ Replace[lis, l_List :> Hold[l], -2]
â Carl Woll
3 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182646%2fflatten-all-inner-lists-that-do-not-have-sublists%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password