Manipulating list with sub-lists of different lengths
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
Consider the following list, which is composed of sublists with different lengths
, 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3
How can this list be transformed into a list of the first elements, second elements, etc...i.e, the out come I would like to produce
2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8
list-manipulation
add a comment |Â
up vote
5
down vote
favorite
Consider the following list, which is composed of sublists with different lengths
, 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3
How can this list be transformed into a list of the first elements, second elements, etc...i.e, the out come I would like to produce
2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8
list-manipulation
related: Transpose uneven lists
â user1066
Aug 20 at 7:40
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
Consider the following list, which is composed of sublists with different lengths
, 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3
How can this list be transformed into a list of the first elements, second elements, etc...i.e, the out come I would like to produce
2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8
list-manipulation
Consider the following list, which is composed of sublists with different lengths
, 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3
How can this list be transformed into a list of the first elements, second elements, etc...i.e, the out come I would like to produce
2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8
list-manipulation
list-manipulation
asked Aug 19 at 14:27
jarhead
689413
689413
related: Transpose uneven lists
â user1066
Aug 20 at 7:40
add a comment |Â
related: Transpose uneven lists
â user1066
Aug 20 at 7:40
related: Transpose uneven lists
â user1066
Aug 20 at 7:40
related: Transpose uneven lists
â user1066
Aug 20 at 7:40
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
Maybe this way?
data = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3;
get[a_, k_] := Map[If[Length[#] >= k, #[[k]], Nothing] &, a];
get[data, 1]
get[data, 2]
get[data, 3]
get[data, 4]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3
7, 2, 5, 5, 4, 7
4, 8
add a comment |Â
up vote
6
down vote
You can use Flatten
:
list =
,
2,2,7,2,
6,3,
5,4,5,5,
4,6,4,7,4,8,
,
6,3
;
Flatten[list, 2]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8
add a comment |Â
up vote
4
down vote
lst = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5,
4, 6, 4, 7, 4, 8, , 6, 3;
Extract[lst, #] & /@ GatherBy[Position[lst, _, 2, Heads -> False], Last]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3,
7, 2, 5, 5, 4, 7,
4, 8
or
parts[l_][k_] := Join @@ MapIndexed[If[Last[#2] == k, #, ## &] &, l, 2]
parts[lst] /@ Range[4]]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3,
7, 2, 5, 5, 4, 7,
4, 8,
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Maybe this way?
data = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3;
get[a_, k_] := Map[If[Length[#] >= k, #[[k]], Nothing] &, a];
get[data, 1]
get[data, 2]
get[data, 3]
get[data, 4]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3
7, 2, 5, 5, 4, 7
4, 8
add a comment |Â
up vote
4
down vote
accepted
Maybe this way?
data = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3;
get[a_, k_] := Map[If[Length[#] >= k, #[[k]], Nothing] &, a];
get[data, 1]
get[data, 2]
get[data, 3]
get[data, 4]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3
7, 2, 5, 5, 4, 7
4, 8
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Maybe this way?
data = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3;
get[a_, k_] := Map[If[Length[#] >= k, #[[k]], Nothing] &, a];
get[data, 1]
get[data, 2]
get[data, 3]
get[data, 4]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3
7, 2, 5, 5, 4, 7
4, 8
Maybe this way?
data = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5, 4, 6, 4, 7, 4, 8, , 6, 3;
get[a_, k_] := Map[If[Length[#] >= k, #[[k]], Nothing] &, a];
get[data, 1]
get[data, 2]
get[data, 3]
get[data, 4]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3
7, 2, 5, 5, 4, 7
4, 8
answered Aug 19 at 14:35
Henrik Schumacher
38.7k253114
38.7k253114
add a comment |Â
add a comment |Â
up vote
6
down vote
You can use Flatten
:
list =
,
2,2,7,2,
6,3,
5,4,5,5,
4,6,4,7,4,8,
,
6,3
;
Flatten[list, 2]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8
add a comment |Â
up vote
6
down vote
You can use Flatten
:
list =
,
2,2,7,2,
6,3,
5,4,5,5,
4,6,4,7,4,8,
,
6,3
;
Flatten[list, 2]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8
add a comment |Â
up vote
6
down vote
up vote
6
down vote
You can use Flatten
:
list =
,
2,2,7,2,
6,3,
5,4,5,5,
4,6,4,7,4,8,
,
6,3
;
Flatten[list, 2]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8
You can use Flatten
:
list =
,
2,2,7,2,
6,3,
5,4,5,5,
4,6,4,7,4,8,
,
6,3
;
Flatten[list, 2]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3, 7, 2, 5, 5, 4, 7, 4, 8
answered Aug 19 at 15:00
Carl Woll
57.7k273149
57.7k273149
add a comment |Â
add a comment |Â
up vote
4
down vote
lst = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5,
4, 6, 4, 7, 4, 8, , 6, 3;
Extract[lst, #] & /@ GatherBy[Position[lst, _, 2, Heads -> False], Last]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3,
7, 2, 5, 5, 4, 7,
4, 8
or
parts[l_][k_] := Join @@ MapIndexed[If[Last[#2] == k, #, ## &] &, l, 2]
parts[lst] /@ Range[4]]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3,
7, 2, 5, 5, 4, 7,
4, 8,
add a comment |Â
up vote
4
down vote
lst = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5,
4, 6, 4, 7, 4, 8, , 6, 3;
Extract[lst, #] & /@ GatherBy[Position[lst, _, 2, Heads -> False], Last]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3,
7, 2, 5, 5, 4, 7,
4, 8
or
parts[l_][k_] := Join @@ MapIndexed[If[Last[#2] == k, #, ## &] &, l, 2]
parts[lst] /@ Range[4]]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3,
7, 2, 5, 5, 4, 7,
4, 8,
add a comment |Â
up vote
4
down vote
up vote
4
down vote
lst = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5,
4, 6, 4, 7, 4, 8, , 6, 3;
Extract[lst, #] & /@ GatherBy[Position[lst, _, 2, Heads -> False], Last]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3,
7, 2, 5, 5, 4, 7,
4, 8
or
parts[l_][k_] := Join @@ MapIndexed[If[Last[#2] == k, #, ## &] &, l, 2]
parts[lst] /@ Range[4]]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3,
7, 2, 5, 5, 4, 7,
4, 8,
lst = , 2, 2, 7, 2, 6, 3, 5, 4, 5, 5,
4, 6, 4, 7, 4, 8, , 6, 3;
Extract[lst, #] & /@ GatherBy[Position[lst, _, 2, Heads -> False], Last]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3,
7, 2, 5, 5, 4, 7,
4, 8
or
parts[l_][k_] := Join @@ MapIndexed[If[Last[#2] == k, #, ## &] &, l, 2]
parts[lst] /@ Range[4]]
2, 2, 6, 3, 5, 4, 4, 6, 6, 3,
7, 2, 5, 5, 4, 7,
4, 8,
edited Aug 19 at 15:22
answered Aug 19 at 14:54
kglr
161k8185384
161k8185384
add a comment |Â
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%2f180244%2fmanipulating-list-with-sub-lists-of-different-lengths%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
related: Transpose uneven lists
â user1066
Aug 20 at 7:40