mercredi 4 décembre 2019

Python Iterating an Array items and registering each item only twice in random

I have been trying to iterate through an array.

below is the code.

x = ['lemon', 'tea', 'water']

def randomShuffle (arr,n):
    from random import choices
    newList=[]


    for item in arr:
        r=choices(arr, k=n)

        if r.count(item) <= 2:
            newList.append(item)

        return (newList)

and the result i want is as below.

randomShuffle(x,6)

['water', 'lemon', 'lemon', 'tea', 'tea', 'water']

i would like to know the logic for writing it please.

thank you all




Aucun commentaire:

Enregistrer un commentaire