1
$\begingroup$

I have a list,

list={"A","B",{{{{{"C"}}}}}, D, E , F , G}

and I want to choose the 'C' (i.e. the first element in the sixth level of the third element in 'list'), which is

list[[3, 1, 1, 1, 1, 1]]

and this gives

C

My question is how can I change the five '1' into a shorter way (use 'i' or something).
I tried

list[[3, ConstantArray[1, 5]]]

I know this is probably wrong because the ConstantArray gives a list, but it gives

{{{{{"C"}}}}, {{{{"C"}}}}, {{{{"C"}}}}, {{{{"C"}}}}, {{{{"C"}}}}}

Why is that?
And how can I make it right?
And in general, how can we make a sequence of repeated numbers, like 1,1,1,1,1 not in a list format (without {})?

Thanks for help!

$\endgroup$
4
  • 1
    $\begingroup$ How about First@Cases[list, "C", Infinity]? $\endgroup$
    – march
    Mar 8, 2017 at 18:13
  • $\begingroup$ @march Yes, it works, thanks! But what the 'Infinity' is doing here, does it represent 'all levels'? (but I tried several number instead, which doesn't work, so I guess it has a different meaning?) And is '@' the same as '@@'? Sorry I'm just a beginner in Mathematica.. $\endgroup$
    – Lazer
    Mar 8, 2017 at 18:23
  • 1
    $\begingroup$ For the symbols, see here. The Infinity means to search on all levels (except level 0, I think). In general, Cases[expr, patt, level] means to search on all levels down to level. If you do First@Cases[list, "C", 6], it returns "C", but not if you put 5 in place of 6. $\endgroup$
    – march
    Mar 8, 2017 at 18:28
  • $\begingroup$ @march Thanks so much! I was just struggling with all those symbols $\endgroup$
    – Lazer
    Mar 8, 2017 at 18:31

1 Answer 1

2
$\begingroup$
list[[3, Sequence @@ ConstantArray[1, 5]]]

"C"

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.