vendredi 19 novembre 2021

How to take items in a list and sort them as a dictionary key with the amount of duplicates from the list as the value? [closed]

I am wanting to sort the items in my randomly generated list into a dictionary with the duplicates increasing the value of the key they are represented by.

Example of what I want dictionary to look like from the list (as I'm struggling to explain what I'm struggling with):

list = [1,1,1,2,2,3,3,3,3,4,5,5]
dict = {
   "1": "3",
   "2": "2",
   "3": "4",
   "4": "1",
   "5": "2",
}

this is the code ive written for generating my list:

import random

delivery_ids = [i for i in range(100)]
del (delivery_ids[random.randrange(len(delivery_ids))])
whileloop = (0)
while whileloop <= 99:
    delivery_ids.extend([whileloop] * random.randint(1, 19))
    whileloop = (whileloop + 1)
random.shuffle(delivery_ids)
print(delivery_ids)



Aucun commentaire:

Enregistrer un commentaire