1

I want to create an array 10*10 (basically 100 elements letter on I can reshape it to 10*10) which has to contain random letter from alphabet.

For example:

array_box = [the first elemet (c), second (e),...100(f)]
2
  • Please clarify your question. Do you want a 10 x 10 array (2-dimensional array) or an array of 100 elements? Commented Jan 31, 2018 at 2:33
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. Commented Jan 31, 2018 at 2:52

2 Answers 2

1

That can be done with a couple of comprehensions like:

Code:

letters = [[random.choice('abcdefghijklmnopqrstuvwxyz') 
            for i in range(10)] for j in range(10)]

Test Code:

import random

letters = [[random.choice('abcdefghijklmnopqrstuvwxyz')
            for i in range(10)] for j in range(10)]
print(letters)

Results:

[
    ['g', 'r', 'r', 'g', 'q', 'h', 'n', 'u', 'g', 's'], 
    ['c', 'm', 'g', 'b', 'z', 'g', 'd', 'm', 'x', 'x'], 
    ['r', 'j', 'e', 'c', 'h', 'm', 'q', 'i', 'c', 'm'], 
    ['v', 'w', 'i', 'x', 'x', 'b', 'l', 'f', 'b', 'x'], 
    ['r', 'r', 'c', 'm', 'f', 'g', 'x', 'z', 'b', 'a'], 
    ['j', 's', 'g', 'n', 'q', 'a', 'f', 'v', 'c', 'o'], 
    ['g', 'r', 'o', 'd', 't', 'n', 'b', 'l', 'h', 'z'], 
    ['h', 'p', 'y', 's', 'k', 't', 'u', 'b', 'n', 'q'], 
    ['u', 'b', 'y', 'z', 'q', 't', 'o', 's', 'l', 'c'], 
    ['w', 'e', 'v', 'p', 'o', 'r', 'f', 'm', 'm', 'h']
]
Sign up to request clarification or add additional context in comments.

1 Comment

You can use random.choice(string.ascii_lowercase) instead of hardcoding all alphabets.
0
>>> import random
>>> import string
>>> s = string.letters[:26]
>>> [[random.choice(s) for i in range(10)] for i in range(10)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.