英文:
Most efficient way of writing code for this specific case
问题
我有一个包含10000个元素的列表,这些元素的值为$10、$100或$1000。
每个元素都被分配一个在0到1000万之间的随机数,不能有两个元素被分配相同的数字。
然后,我需要根据它们的数字将这些元素分成不同的列表。数字在0-999之间的元素放在一个列表中,1000-1999之间的放在另一个列表中,以此类推。然后,我们计算每个列表中的金额总和。
例如:
元素列表:[$10, $100, $100, $1000, $10, $100, $100]
随机唯一数字(使用0和10k而不是1000万):
[35, 9999, 4, 3001, 6005, 7865, 3434]
在分割和求和后的输出结果(按照1000的区间划分):
[$110, $0, $0, $1100, $0, $0, $10, $100, $0, $100]
如上所示,第一个元素是$110,因为在0-999之间有2个元素,它们分别是$100和$10,相加得到$110。第二个区间1000-1999为空,因为在这个范围内没有数字。
我询问效率问题,因为实际数字要大得多。
如何使用纯Python相对高效地完成这个任务?
英文:
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?
答案1
得分: 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
.
英文:
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
.
答案2
得分: 0
我猜你有两个列表,一个保存值($10,$100,$1000),
另一个保存索引。
如果这两个列表的大小相同,你可以这样做:
for i in range(0, len(ls_values)):
if ls_indexes[i] <= 10:
ls_tens.append(ls_values[i])
# 其他条件用于处理100和1000
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论