0
import random

M = 4
N = 3


def generisanjevol1(nekalista, m):
    return random.choices(nekalista, k=m)


def generisanjevol2(nekalista, m,n):
    #obj = [[random.choice(nekalista,k=m)] for i in range(N)]]
    obj = [[random.choice(nekalista)] for i in range(n)]
    return obj

    #def poredjenje()


listaslova = ['A', 'B', 'C', 'D', 'E']

lista = generisanjevol1(listaslova, M)
lista2 = generisanjevol2(listaslova, M, N)
print(lista)
print(lista2)

So above is my try (generisanjevol2(nekalista, m,n)...

What I am trying to do is next: I want to generate N of arrays and fill them with strings which are generated by random.choice function and they still must be strings from listaslova)
Perhaps let's say N=3 (N represents numbers of arrays) and M=4 (M represents length of array) I should get something like this (doesn't have to be same data in arrays, because of course they are randomly generated):

[A,C,D,E]
([A,C,E,D] [E,C,B,A] [E,D,D,A])

But the results which I get are following:

[A,D,E,C]
[[B],[D],[E]]

P.S If I try the one which is commented I get an error

2 Answers 2

1

The error in your commented line is because you have an extra ]. And random.choice should be random.choices.

But you also shouldn't put another list around the call to random.choice(). It already returns a list.

def generisanjevol2(nekalista, m,n):
    obj = [random.choices(nekalista,k=m) for i in range(n)]
    return obj
Sign up to request clarification or add additional context in comments.

14 Comments

In commented line I get error with random.choice(nekalista, k=m)... It says following: TypeError: choice() got an unexpected keyword argument 'k'
It should be random.choices, just like in generisanjevol1
By calling random.choices() in generisanjevol2, like I showed in the answer.
Did you see that I edited the answer after you posted your comment?
Write a function that compares the lists and returns a score of how similar they are. Then you can use sorted(zip(lista2, lista2), key=lambda pair: yourFunction(*pair)).
|
1

Like @Barmar said, you indeed have an extra []. Your function should look:

def generisanjevol2(nekalista, m,n):
...:     obj = [random.choices(nekalista, k=m) for i in range(n)]
...:     return obj

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.