-1

I have 10000 elements in a list that has the value $10, $100 or $1000.

Each element is given a random number between 0 and 10 million, no 2 elements can have the same assigned the same numbet.

I then have to split the elements based on their number. So dollar amounts with a number between 0-999 are in one list, 1000-1999 in another etc. Then we sum the amount of money in each list.

For example:

Element list: [$10,$100,$100,$1000,$10,$100,$100]

Random unique numbers (using 0 and 10k instead of 10mil): [35,9999,4,3001,6005,7865,3434]

Output after splitting and summing on 1000 bins: [$110,$0,$0,$1100,$0,$0,$10,$100,$0,$100]

As seen above, first element is $110 because there are 2 elements with numbers between 0-999,and they add up by $100+$10. 2nd bin 1000-1999 is empty because there are no numbers in this range.

I am asking about efficiency because actual numbers are much much larger.

How can I do this relatively efficiently using raw python?

pppery
  • 3,731
  • 22
  • 33
  • 46
Rezzy
  • 111
  • 1
  • 2
  • 10
  • 2
    What have you tried so far? Also, I believe you probably can't make this very slow even with a naive/ dumb approach, so for this simple task with just 10.000 elements the algorithm y won't really matter much. – Mushroomator Jul 17 '23 at 22:14
  • 3
    If you have only 10000 elements, then I don't see how any of them could have an index of 10 million. Unless you intend "index" to mean something other than "position in the list". – John Gordon Jul 17 '23 at 22:14
  • Sounds like a job for `defaultdict(list)` with keys being `index // 1000` – Homer512 Jul 17 '23 at 22:19
  • @Homer512: Or if the data is in order, [just iterate over it in chunks](https://stackoverflow.com/q/434287/364696). The OP's question is just too vague to answer without greater details, and practically, needs a [MCVE] to clearly indicate the desired behavior for a given set of inputs. – ShadowRanger Jul 17 '23 at 22:23
  • 1
    Don't worry about efficiency (yet). First, make it work. Then, see if it is fast enough. You haven't done the first part yet. – Tim Roberts Jul 17 '23 at 22:26

2 Answers2

1

Create a set of bins. If each bin is 1000 units wide, then you need 10,000 bins. Now, you can parcel out each value into the right bin according to its index. Then, you just have to sum up the bins.

bins = [[] for _ in range(10000)]

for ele,val in zip (indices,values):
    bins[ele//1000].append(val)

for n,b in enumerate(bins):
    dol = n * 1000
    print(f'Sum between {dol} and {dol+999}: {sum(b)}')

I'm assuming here the indexes are actually between 0 and 9,999,999. If they really are 1 and 10,000,000, then use (ele-1)//1000.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

I guess you have two list, one that holds the values ($10, $100, $1000) And the other list that holds the indexes.

If both lists are the same size, you can do something like:

for i in range(0, len(ls_values)):
    if ls_indexes[i] <= 10:
        ls_tens.append(ls_values[i]
    # Other conditions for the 100s and the 1,000s
lapdoggo
  • 3
  • 2
  • VERY bad to have 2 lists, we are not using BASIC anymore – rioV8 Jul 17 '23 at 23:32
  • @rioV8: What does that even mean? The design requires more than one `list`. I think you're saying you don't need a sideband `list` for tracking supplementary data, but also, "VERY bad" is overstating things, and BASIC feels utterly irrelevant. The answer isn't great (it's not even syntactically valid, and doesn't really seem to be answering the question as asked), but your objections seem to be going off on a *weird* tangent. – ShadowRanger Jul 17 '23 at 23:58