mardi 9 août 2016

Python - create a third array using two arrays

I have to two arrays (in my code below): "donlist" is a list of random donation amounts between $1 and $100, and "charlist" is a list of random charity numbers between 1 and 15. I need to create a third array using the total donations for each charity. So if charity #3 appears 8 times in "charlist", I have to get the sum of the corresponding floats from "donlist". I have absolutely no idea how to do this and have been trying to figure it out for the past 2-3 hours now. Does anyone know how to do it? Thank you.

import random
from array import *

counter = 0
donlist = []
charlist = []

while counter != 100:
    d = random.uniform(1.00,100.00)
    c = random.randint(1,15)
    counter +=1
    donlist.append(d)
    donlist = [round(elem,2) for elem in donlist]
    charlist.append(c)
    if counter == 100:
        break




Aucun commentaire:

Enregistrer un commentaire