Message Boards Message Boards

0
|
7685 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Convert a grid of lists to a grid of strings?

Posted 8 years ago

For the problem in the Elementary Wolfram Language book (Chapter: Arrays, of Lists of Lists): "Make a grid of all possible strings consisting of pairs of letters of the alphabet (“aa”, “ab”, etc.)", an answer could be:

Grid[Table[{Part[Alphabet[], i], Part[Alphabet[], j]}, {i, 26}, {j,26}]]

However, this produces an answer which is a set of lists: {a,a}...and not the preferred: aa... Is there a way to convert a grid of lists to a grid of strings? Thank you.

POSTED BY: Arthur Hill
4 Replies

It can be done much easier:

Grid[Table[StringJoin[i, j], {i, Alphabet[]}, {j, Alphabet[]}]]

Note that i (and j) now iterate over a...z not from 1...26. Then a simple StringJoin, and done.

POSTED BY: Sander Huisman
Posted 8 years ago

Henrik: Thank you for your solution to my elementary coding problem. Having reviewed your "staff picks" item: "Convert 2D into a 3D object: radiotherapy treatment planning system", it becomes clear that coding at that level can result in compact elegant method.

POSTED BY: Arthur Hill
Posted 8 years ago

There are number of ways to do it-

Grid[Map[StringJoin,Table[{Alphabet[][[i]], Alphabet[][[j]]}, {i, 26}, {j, 26}], {2}]]

or you can write-

Grid[Partition[StringJoin/@Tuples[{Alphabet[], Alphabet[]}], 26]] 
POSTED BY: Girish Arabale

How about:

letters = CharacterRange["a", "z"];
Grid@Outer[#1 <> #2 &, letters, letters]
POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract