dimanche 30 octobre 2022

n*n array which has 2 times every random values

To randomly generate a n*n array of assorted characters such that there are exactly two of each character in PYTHON


import random import numpy as np from string import punctuation set(punctuation) S = 5 char = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] Digits = [0,1,2,3,4,5,6,7,8,9] symbols = ['~', ':', '+', '[', '@', '^', '{', '%', '(', '-', '*', '|', '&', '<', '}',
'_', '=', ']', '!', '>', ';', '?', '#', '$', ')', '/']
def create_array(n): s =random.choices(char + Digits + symbols) print(s) x = np.empty((n, n), dtype=object) if(n<=100):

    for i in range(n): 
        for j in range(n): 
            k =random.choices(char + Digits + symbols)
            x[i][j] = k              
    for i in range(n): 
        for j in range(n): 
            print(*x[i][j],sep=" ",end = " ")  
        print() 

n = 5

create_array(n)

i don't know how to assign the same value a second time



Aucun commentaire:

Enregistrer un commentaire